"Unexpected token , in JSON at position N"
Almost always a trailing comma — a comma left after the last item in an object or array:
{"a": 1, "b": 2,}
JSON doesn't allow a trailing comma before a closing } or ], unlike JavaScript object/array literals, which do. Remove the comma immediately before the closer.
"Unexpected token ' in JSON at position N" (or similar, for a quote character)
Caused by single quotes instead of double quotes — {'name': 'x'} is invalid; JSON requires double quotes for both keys and string values, with no exception.
"Unexpected end of JSON input"
The document ended before the parser found a matching closer for every open { or [ — a missing closing brace or bracket somewhere, often deep in a nested structure. This error gives no clue where the missing closer should go; working from the innermost nesting level outward, counting opens against closes, is the most reliable way to locate it in a large document.
"Unexpected number in JSON at position N"
Usually a leading zero (007) or a leading plus sign (+5) — both invalid in JSON numbers, even though they might be valid in other contexts. Strip the leading zero/plus, or wrap the value in quotes if it's meant to be a string (like a zip code) rather than a number.
"Unexpected token u in JSON at position N" (or similar with 'N', 'a', etc.)
Almost always undefined, NaN, or Infinity appearing somewhere in the document — none of these are valid JSON values, even though they're valid in the JavaScript the format is based on. undefined typically sneaks in when data is serialized from a JavaScript object that had an actual undefined property value written out incorrectly by hand, rather than omitted (which is what JSON.stringify does automatically).
A quick diagnostic table
| Error pattern | Usual cause | Fix |
|---|---|---|
Unexpected token , |
Trailing comma | Remove the comma before }/] |
Unexpected token ' |
Single quotes used | Use double quotes only |
Unexpected end of JSON input |
Missing closing brace/bracket | Count opens vs. closes from the innermost level out |
Unexpected number |
Leading zero or + sign |
Strip the leading character, or quote as a string |
Unexpected token u/N/I |
undefined/NaN/Infinity present |
Replace with null or a valid number |
Common mistakes
- Fixing the reported position without checking nearby context. For nested-structure errors especially, the actual mistake is often just before the reported position, not exactly at it.
- Assuming a document that "looks fine" is valid. Trailing commas and single quotes are easy to miss on a quick visual scan, especially in large, deeply nested JSON.
- Hand-editing minified JSON to fix an error. Format it first — pretty-printed JSON makes bracket matching and comma placement dramatically easier to verify by eye.
FAQ
Why does my JSON fail with "Unexpected token ," specifically?
A trailing comma before a closing } or ] — valid in JavaScript object/array literals, invalid in JSON.
What does "Unexpected end of JSON input" mean?
The parser reached the end of the string still expecting a closing brace or bracket that never appeared — a mismatched nesting depth somewhere in the document.
Can undefined appear in valid JSON?
No — undefined, NaN, and Infinity are all invalid JSON values, even though they're valid in plain JavaScript; use null or a real number instead.
Format and validate JSON to catch these errors instantly, with the exact line highlighted, using the JSON Toolkit.