9 Common JSON Syntax Errors and How to Fix Them
JSON looks like JavaScript, but it is much stricter. Most "invalid JSON" errors come from a short list of mistakes, usually from writing JSON by hand, copying it from code, or receiving it from a system that does not follow the standard. Here are the nine most common, each with a broken and a fixed example.
1. Trailing commas
A comma after the last item in an object or array is not allowed.
โ { "name": "Ann", "age": 30, }
โ [1, 2, 3,]
โ { "name": "Ann", "age": 30 }
โ [1, 2, 3]2. Single quotes
Both keys and string values must use double quotes. Single quotes and backticks are not valid.
โ { 'city': 'Cairo' }
โ { "city": "Cairo" }3. Unquoted keys
In JavaScript you can write keys without quotes. In JSON every key is a string in double quotes.
โ { id: 7, active: true }
โ { "id": 7, "active": true }4. Comments
JSON has no comments. //, /* */ and # all cause errors. (Some tools, like VS Code settings, accept a variant called JSONC that allows comments, but standard parsers do not.)
โ {
// server port
"port": 8080
}
โ { "port": 8080 }If you need to document a value, add a regular field such as "_comment": "server port", or use a format that supports comments, like YAML.
5. Missing commas
Easy to miss when you add a line to a multi-line object:
โ {
"a": 1
"b": 2
}
โ {
"a": 1,
"b": 2
}The error is reported on the line after the missing comma, at "b", because that is where the parser realizes something is wrong.
6. Special values: NaN, Infinity, undefined
JSON supports only strings, numbers, true, false, null, objects and arrays. NaN, Infinity and undefined are not valid. Python's True, False and None are not valid either.
โ { "score": NaN, "ok": True, "note": None }
โ { "score": null, "ok": true, "note": null }json.dumps() writes NaN by default, which other languages then reject. Pass allow_nan=False to catch this early, and convert such values to None yourself.7. Line breaks and tabs inside strings
A string cannot contain a raw line break or tab. They must be escaped:
โ { "text": "line one
line two" }
โ { "text": "line one\nline two" }The same goes for a double quote inside a string: write \". Backslashes in Windows paths must be doubled: "C:\\Users\\ann".
8. Invalid numbers
JSON numbers cannot have leading zeros, a leading plus sign, a leading or trailing dot, or hexadecimal notation.
โ { "a": 007, "b": +3, "c": .5, "d": 5., "e": 0xFF }
โ { "a": 7, "b": 3, "c": 0.5, "d": 5, "e": 255 }If a value like a zip code or a phone number needs its leading zeros, store it as a string: "zip": "01234".
9. Extra content around the JSON
The text must contain exactly one JSON value. These all fail:
- A JavaScript assignment:
const data = { ... }; - A JSONP wrapper:
callback({ ... }) - Markdown code fences copied from a chat or documentation (three backticks)
- Several objects one after another, like in log files. That format is called JSON Lines (NDJSON) and has to be read line by line, or wrapped in an array.
- An invisible byte-order mark (BOM) at the very start of a file saved by some Windows editors
How to find and fix errors fast
- Paste the text into the JSON Validator. It shows the exact line and column and explains the problem in plain English.
- If there are many problems, run it through JSON Repair. It fixes all nine mistakes above and lists every change it made. Long numbers keep all their digits.
- Prevent errors at the source: generate JSON with
JSON.stringify()orjson.dumps()instead of building strings by hand.
Seeing a specific error message? See Unexpected token in JSON for what each message means.