Invalid JSON: The 10 Most Common Errors and How to Fix Them
JSON is strict. A file that looks fine to a person can be rejected by a parser because of one stray character, and the error message rarely says what is wrong in plain words. These ten problems account for most failures.
The ten problems
| Problem | Invalid | Fix |
|---|---|---|
| 1. Trailing comma | {"a": 1,} | Remove the last comma |
| 2. Single quotes | {'a': 1} | Use double quotes only |
| 3. Unquoted keys | {a: 1} | Write "a" |
| 4. Comments | // note or /* note */ | Delete them; JSON has no comments |
| 5. Missing comma | {"a": 1 "b": 2} | Add a comma between pairs |
| 6. Special values | NaN, Infinity, undefined | Use null or a string |
| 7. Python values | True, False, None | Use true, false, null |
| 8. Bad string content | A raw line break or an unescaped quote inside text | Write \n and \" |
| 9. Bad numbers | 007, .5, 0x1F, +3 | Write 7, 0.5, 31, 3 |
| 10. Extra content | Two objects in a row, or text after the closing bracket | Wrap in an array, or see JSON Lines |
The invisible ones
Some files fail at position 0 with no visible cause. A byte order mark, a few hidden bytes some editors add at the start of a UTF-8 file, will do that, as will a stray character copied from a web page. Curly quotes from a word processor look like quotes but are different characters. Re-save the file as plain UTF-8 and retype the quotes.
Where the bad JSON comes from
- JavaScript objects and Python dictionaries printed as text. They look like JSON but use single quotes, unquoted keys or Python constants. Serialize them with
JSON.stringifyorjson.dumpsinstead of copying the printed output. - Config files that allow comments and trailing commas, such as JSONC.
- Log files and exports with one object per line. That is JSON Lines, not one JSON document; see JSONL vs JSON.
- Cut-off downloads, where the file simply ends too early and a bracket is missing.
How to find the exact spot
Parsers report a position or a line and column, and the real mistake is usually just before it, since the parser only notices when it meets something unexpected. Paste the text into the JSON formatter: it shows the line and column with the surrounding text, and can repair many of the problems above, listing each change it made so you can check it. For the most common error message, read unexpected token in JSON.
After it is valid
Once the JSON parses, you can compare versions with the JSON Diff tool or check its structure with a schema; see JSON Schema basics.