How to Fix "Unexpected Token" JSON Parse Errors

If you have ever called JSON.parse() and gotten back something like SyntaxError: Unexpected token ',', ...is not valid JSON, you already know the error message tells you almost nothing useful on its own. It points at a character, not at the actual mistake. The good news is that the vast majority of these errors come from a small, repeatable set of causes, and once you know what they look like you can usually spot the problem before your editor even highlights it.

Trailing commas

This is the single most common one. JavaScript object and array literals happily allow a trailing comma after the last item, so it is easy to write JSON the same way out of habit:

{
  "name": "Freddy",
  "role": "developer",
}

That trailing comma after "developer" is invalid JSON, full stop. Strict JSON has no concept of a trailing comma at all. The fix is simply to remove it, but if you are generating JSON programmatically by concatenating strings in a loop, this is worth checking for specifically, since it is an easy mistake to bake into a template.

Unquoted or single-quoted keys

JavaScript object literals allow unquoted keys and single-quoted strings. JSON allows neither. Keys and string values must use double quotes, no exceptions:

{ name: 'Freddy' }     // invalid JSON
{ "name": "Freddy" }   // valid JSON

This one shows up most often when someone copies a JavaScript object literal straight out of source code and expects it to parse as JSON without changes.

Comments

JSON has no comment syntax whatsoever. Not //, not /* */. If a config file or sample payload was copied from a JSON5 or JSONC source (both of which do allow comments), stripping those comments out is a required step before it will parse as plain JSON.

A missing closing brace or bracket

On deeply nested objects this is easy to lose track of by eye. The parser error for this one often points at the very end of the string, or sometimes at a position that looks completely unrelated to the actual missing brace, because the parser only realizes something is wrong once it runs out of input while still expecting more. Counting opening and closing braces in pairs, working from the outside in, is usually faster than reading top to bottom when you are hunting for this.

Using undefined or a function as a value

This one is specific to building JSON from JavaScript data. JSON.stringify() will silently drop object keys whose value is undefinedor a function, rather than throwing, which is a common source of confusion when a field you expected to see is just missing from the output. It is not a parse error exactly, since nothing fails, but it explains a lot of "why is this field gone" questions people have when debugging serialized data.

The fastest way to actually find the mistake

Rather than staring at raw text trying to spot a missing comma, paste the payload into a formatter that highlights the exact error location. The JSON Formatter on this site runs entirely in your browser and reports the parse error immediately, without sending your data anywhere first, which matters if the payload you are debugging happens to contain a real API response with tokens or internal data in it.

← Back to KeyForge