Common JSON Errors and How to Fix Them
JSON looks like JavaScript, but it is stricter. Almost every “Unexpected token” error comes from one of a handful of habits that JavaScript allows and JSON does not.
The usual suspects
| Mistake | Invalid | Valid |
|---|---|---|
| Trailing comma | {"a": 1,} | {"a": 1} |
| Single quotes | {'a': 1} | {"a": 1} |
| Unquoted keys | {a: 1} | {"a": 1} |
| Comments | {"a": 1 // note} | Remove comments (or use a format that allows them) |
| NaN / Infinity / undefined | {"a": NaN} | {"a": null} or a string |
| Leading zeros | {"n": 007} | {"n": 7} |
| Raw line break in a string | a string split across lines | write \n inside the string |
| Capitalised literals | True, Null | true, null |
How to find the error fast
- Paste the text into JSON Formatter. It validates as you type and points to the line and column of the first error.
- Look just before the reported position: the parser notices a problem one character after the real mistake (a missing comma shows up at the next key).
- Fix and repeat — there may be more than one error.
Subtle problems that still parse
- Duplicate keys like
{"a":1,"a":2}. RFC 8259 says names SHOULD be unique; different parsers keep the first or the last value. Avoid them. - Big integers above 2^53 (9,007,199,254,740,992) lose precision in JavaScript. Send IDs as strings.
- A byte-order mark at the start of a file can break some parsers. Save files as UTF-8 without BOM.
When JSON is the wrong format
If you need comments or trailing commas in a configuration file, use a format designed for it (YAML, TOML or JSON5) and convert to JSON for machines. YAML to JSON does that conversion in your browser.