Common JSON errors and how to fix them
JSON looks like JavaScript, but its rules are much stricter. Most “Unexpected token” and “invalid JSON” errors come from a short list of mistakes. Here they are, with an invalid example, the fixed version and the reason behind each rule.
Trailing commas
JavaScript allows a comma after the last item in an object or array. JSON does not, and this is the single most common reason a parser rejects a file.
✗ Invalid{
"name": "Asha",
"city": "Pune",
}
✓ Valid{
"name": "Asha",
"city": "Pune"
}
Fix: Remove the comma after the last property or array item.
Single quotes instead of double quotes
JSON strings and property names must use double quotes. Single quotes are common when JSON is written by hand or copied from Python or JavaScript code.
✗ Invalid{'name': 'Asha'}
✓ Valid{"name": "Asha"}
Fix: Replace every single quote that delimits a string or key with a double quote. If the text itself contains a double quote, escape it as \".
Unquoted property names
JavaScript object literals allow bare keys like name:. JSON requires every key to be a quoted string.
{name: "Asha", age: 31}
✓ Valid{"name": "Asha", "age": 31}
Fix: Wrap each key in double quotes.
Comments
Standard JSON has no comment syntax, so // and /* */ make the document invalid — a frequent problem in configuration files.
{
// port for the dev server
"port": 8080
}
✓ Valid{
"_comment": "port for the dev server",
"port": 8080
}
Fix: Delete the comments, or move the note into a regular property such as "_comment". Some tools accept JSONC or JSON5, but a strict parser will not.
undefined, NaN and Infinity
These are valid in JavaScript but not in JSON. They often appear when an object is printed with a custom serializer or copied from a browser console.
✗ Invalid{"discount": undefined, "ratio": NaN}
✓ Valid{"discount": null, "ratio": null}
Fix: Use null, remove the property, or store the value as a string such as "NaN" if the receiver expects it.
Missing commas between items
When properties are added or reordered by hand, it is easy to leave out the comma between two of them. The parser then reports an unexpected string or token on the next line.
✗ Invalid{
"name": "Asha"
"city": "Pune"
}
✓ Valid{
"name": "Asha",
"city": "Pune"
}
Fix: Add a comma after every property except the last. Good error messages point to the start of the line after the missing comma.
Line breaks and tabs inside strings
A string must stay on one line. Raw line breaks and tab characters inside quotes are control characters and are not allowed.
✗ Invalid{"note": "Line one
Line two"}
✓ Valid{"note": "Line one\nLine two"}
Fix: Write line breaks as \n and tabs as \t. Most languages do this automatically when they serialize JSON.
Invalid backslash escapes (Windows paths)
Inside a JSON string, a backslash starts an escape sequence. Only \" \\ \/ \b \f \n \r \t and \uXXXX are allowed, so Windows paths break easily.
{"path": "C:\Users\asha\data.json"}
✓ Valid{"path": "C:\\Users\\asha\\data.json"}
Fix: Double every backslash, or use forward slashes, which Windows also accepts in most APIs.
Leading zeros and number formats
JSON numbers cannot start with a zero (except 0 itself), cannot start with + or a decimal point, and cannot be hexadecimal.
{"pin": 0411, "price": .5, "flags": 0xFF}
✓ Valid{"pin": "0411", "price": 0.5, "flags": 255}
Fix: Store codes with meaningful leading zeros — PIN codes, phone numbers, IDs — as strings, and write decimals with a leading 0.
Unclosed or mismatched brackets
Every { needs a matching } and every [ a matching ]. Truncated API responses and copy-paste mistakes are the usual causes.
{"items": [1, 2, 3}
✓ Valid{"items": [1, 2, 3]}
Fix: Check the structure from the outside in. A formatter that reports where an unclosed bracket was opened makes this quick to find.
More than one top-level value
A JSON document contains exactly one value. Log files often hold one JSON object per line (NDJSON or JSON Lines), which is not a single valid JSON document.
✗ Invalid{"id": 1}
{"id": 2}
✓ Valid[
{"id": 1},
{"id": 2}
]
Fix: Wrap the values in an array, or process the file line by line with a JSON Lines reader.
A quick debugging routine
- Find the exact position. Paste the JSON into a validator that reports the line and column, not just “invalid JSON”.
- Look just before the reported position. The parser fails where it notices the problem, which is often one token after the real mistake — a missing comma is reported on the next line.
- Check the usual suspects in this guide: trailing commas, quotes, comments and backslashes cover most cases.
- Fix at the source. If the JSON is generated by code, build it with your language’s JSON library (
JSON.stringify,json.dumps, Jackson, and so on) instead of concatenating strings.
Check your own JSON or XML in seconds — free, and nothing is uploaded.
Open the formatter →