Common JSON Errors and How to Fix Them
JSON syntax errors are among the most frustrating bugs to debug — a single misplaced comma or wrong quote breaks everything. Here are the 10 most common JSON errors with their exact fixes.
Advertisement
A JSON syntax error fails silently in some environments and explosively in others. 'JSON.parse: unexpected character at line 1 column 92' is one of the least helpful error messages in software development. This guide covers the 10 most common JSON errors, explains exactly why each one fails, and shows the fix.
Debug faster
Paste any broken JSON into a JSON formatter/validator. It will identify the exact line and character position of the first error, turning a 30-minute hunt into a 30-second fix.
1. Trailing Comma
The most common JSON error. A trailing comma appears after the last item in an object or array.
Invalid JSON
{ "name": "Alice", "age": 30, ← trailing comma }
Fix
{ "name": "Alice", "age": 30 }
Why it happens: JavaScript objects and arrays allow trailing commas (ES2017+), so developers copy JavaScript literal syntax into JSON. JavaScript allows `{name: 'Alice', age: 30,}` but JSON strictly doesn't.
2. Single Quotes Instead of Double Quotes
Invalid JSON
{'name': 'Alice'} ← single quotes everywhere
Fix
{"name": "Alice"}
JSON requires double quotes for all strings — both keys and values. Single quotes are not valid JSON. This error comes from developers familiar with Python or JavaScript where both quote types work.
Advertisement
3. Unquoted Keys
Invalid JSON
{name: "Alice"} ← key without quotes
Fix
{"name": "Alice"}
In JavaScript objects, keys can be unquoted identifiers: `{name: 'Alice'}`. In JSON, every key must be a quoted string. Copying object literals from JavaScript code to JSON is the most common source of this error.
4. Comments in JSON
Invalid JSON
{ // This is a comment — INVALID "name": "Alice" }
Fix
{ "name": "Alice" }
JSON has no comment syntax. Neither `//` nor `/* */` are valid in JSON. If you need comments in config files, consider JSONC (JSON with Comments) format if your tools support it, or YAML which natively supports comments.
5. undefined Instead of null
Invalid JSON
{"value": undefined} ← undefined is not JSON
Fix
{"value": null}
undefined is a JavaScript-only concept. JSON does not have an undefined type. Use null to represent the absence of a value in JSON. In JavaScript, JSON.stringify() drops properties with undefined values rather than converting them to null — watch for this when serializing JavaScript objects to JSON.
6. Mismatched Brackets
Invalid JSON
{"data": [1, 2, 3} ← array opened with [ but closed with }
Fix
{"data": [1, 2, 3]}
Every opening bracket/brace must be closed with the matching type: `{` with `}`, `[` with `]`. Deep nesting makes these easy to lose track of. A JSON formatter will immediately show the mismatched bracket position.
7. Unescaped Special Characters
Invalid JSON
{"message": "Say "hello""} ← needs escaping {"path": "C:\Users\Alice"} ← needs escaping
Fix
{"message": "Say \"hello\""} {"path": "C:\\Users\\Alice"}
Inside JSON strings, certain characters must be escaped with a backslash: double quote (\"), backslash (\\), newline (\n), tab (\t), carriage return (\r), and Unicode characters (\uXXXX). Unescaped double quotes inside a string end the string prematurely; unescaped backslashes create invalid escape sequences.
Advertisement
8. Numbers as Object Keys
Invalid JSON
{1: "one", 2: "two"} ← numeric keys are invalid
Fix
{"1": "one", "2": "two"}
JSON object keys must always be strings. Even if the key represents a number conceptually, it must be in double quotes. This can trip up developers working with dynamic key generation.
9. NaN and Infinity
Invalid JSON
{"ratio": NaN, "score": Infinity} ← not valid JSON numbers
Fix
{"ratio": null, "score": null} ← or use a numeric fallback
JSON's number type only supports finite decimal numbers. NaN (Not a Number) and Infinity are JavaScript-specific floating-point representations that don't exist in the JSON spec. JavaScript's JSON.stringify() converts NaN and Infinity to null automatically.
10. JSON Within a JSON String
Invalid JSON (double encoding)
{"data": "{"name": "Alice"}"} ← JSON string inside JSON value
Double encoding happens when JSON is serialized twice — a common bug in APIs that wrap response objects in strings. The fix is to pass the object directly instead of serializing it first, or to deserialize (JSON.parse) the inner string after receiving it. Always validate your API responses to catch double encoding early.
Fix JSON Errors With Our Formatter
Paste broken JSON and get the exact line number and error type. Free, instant.
Advertisement
Frequently Asked Questions
What is a JSON syntax error?
A JSON syntax error occurs when JSON text doesn't conform to the JSON specification (RFC 8259). Common causes: trailing commas, single quotes instead of double quotes, unquoted keys, comments, and mismatched brackets.
How do I find a JSON error?
Use a JSON validator or formatter tool. Paste your JSON and the tool reports the exact line and character position of the first error. In code, wrap JSON.parse() in a try-catch and log the SyntaxError message.
Why does JSON not allow comments?
Douglas Crockford, who formalized JSON, deliberately excluded comments because he observed them being used as parsing directives in other formats, which conflicted with JSON's design goal of being a pure data format. If you need comments, use YAML or JSONC.
What does 'unexpected token' mean in JSON?
'Unexpected token' means the parser found a character it didn't expect at that position. It could be a single quote, an unquoted key, a comment, or a trailing comma. Use a JSON formatter to identify the exact position.
How do I fix a trailing comma in JSON?
Remove the comma after the last item in the object or array. In `{"a":1, "b":2,}`, remove the comma after `2`. Most JSON formatters and linters will flag trailing commas automatically.
Is JSON case-sensitive?
Yes. JSON is case-sensitive. The keywords `true`, `false`, and `null` must be lowercase — `True`, `False`, or `NULL` are invalid JSON. Object keys are also case-sensitive: 'Name' and 'name' are different keys.