How to Fix JSON Parse Errors
JSON is stricter than JavaScript object syntax. Almost every parse error comes from one of a short list of mistakes.
Updated
Try it now: JSON Validator. It runs in your browser, with no signup.
The ten usual causes
- Trailing comma after the last item: {"a": 1,}. Remove the comma.
- Single quotes: {'a': 1}. JSON requires double quotes for keys and strings.
- Unquoted keys: {a: 1}. Every key must be a double-quoted string.
- Comments (// or /* */). JSON has no comment syntax. Strip them, or use a format that allows comments for config files.
- Values that do not exist in JSON: undefined, NaN and Infinity. Use null or a string.
- Unescaped characters inside strings: a raw line break, a tab or a double quote. Write them as \n, \t and \".
- A byte order mark (BOM) at the start of a file, which looks like invisible text before the opening brace.
- Numbers with leading zeros or a leading plus, such as 007 or +5, and hexadecimal literals such as 0xFF.
- Several JSON documents in one file, as in logs with one object per line. Parse each line separately.
- A truncated response, where the closing bracket is missing because the download or copy was cut short.
Find the exact position
Error messages differ between browsers and runtimes, but most point to a position or a line and column. Look at the character just before the reported position as well, because the real mistake is often the comma or quote that came earlier.
// JavaScript
try {
JSON.parse(text);
} catch (e) {
console.error(e.message); // wording differs between browsers
}
# Python reports line and column
import json
try:
json.loads(text)
except json.JSONDecodeError as e:
print(e.msg, "line", e.lineno, "column", e.colno)Check it quickly from the terminal
Paste the text into the JSON Validator to see the parser error, then format it with the JSON Formatter so the structure is easy to read.
# Python's built-in tool: exits with an error and a position if invalid python -m json.tool response.json # jq prints the parse error jq . response.json
Prevent it at the source
- Produce JSON with a serializer, not by joining strings by hand.
- Set the Content-Type header to application/json and check the response status before parsing.
- Read the body as text and log it when a parse fails, so you can see what actually arrived.
Frequently asked questions
Why does JSON.parse fail on a JavaScript object literal?
Object literals allow unquoted keys, single quotes and trailing commas. JSON allows none of them.
Can JSON contain comments?
No. Some config tools accept comments in their own variants, but standard JSON parsers, including JSON.parse, reject them.
Is a plain string or number valid JSON?
Yes. Under the current standard a JSON text can be any value, not only an object or array.

