10 Common JSON Errors and How to Fix Them
Every developer has stared at a "SyntaxError: Unexpected token" message while working with JSON. These errors are frustrating because JSON's strict syntax rules mean that even tiny mistakes โ a missing comma, a wrong quote character, or a trailing comma โ will cause the entire document to fail parsing.
In this article, we'll cover the ten most common JSON errors, explain why they happen, and show you exactly how to fix each one. Bookmark this page โ you'll want to reference it the next time JSON.parse() throws an error.
1. Trailing Commas
This is the single most common JSON error. Unlike JavaScript objects, JSON does not allow trailing commas after the last element.
// โ Invalid - trailing comma after "editor"
{
"name": "Alice",
"roles": ["admin", "editor",]
}
// โ Valid - no trailing comma
{
"name": "Alice",
"roles": ["admin", "editor"]
}
Why it happens: Most programming languages allow trailing commas, so developers add them out of habit. JSON's spec explicitly forbids them.
2. Single Quotes Instead of Double Quotes
JSON requires double quotes for all strings โ both keys and values. Single quotes are not valid.
// โ Invalid - single quotes
{'name': 'Alice'}
// โ Valid - double quotes
{"name": "Alice"}
Why it happens: JavaScript and Python both accept single quotes for strings. When copying objects from code, developers forget to convert them.
3. Unquoted Keys
In JavaScript, object keys don't need quotes. In JSON, every key must be a double-quoted string.
// โ Invalid - unquoted key
{name: "Alice"}
// โ Valid - quoted key
{"name": "Alice"}
4. Comments in JSON
JSON does not support comments of any kind. No //, no /* */, no #.
// โ Invalid - comments not allowed
{
// user's name
"name": "Alice",
/* age in years */
"age": 30
}
// โ Valid - no comments
{
"name": "Alice",
"age": 30
}
Workaround: If you need comments in configuration files, consider JSONC (JSON with Comments) supported by VS Code, or use YAML instead.
5. Missing Commas Between Elements
Forgetting a comma between key-value pairs or array elements is an easy mistake to make.
// โ Invalid - missing comma after "Alice"
{
"name": "Alice"
"age": 30
}
// โ Valid
{
"name": "Alice",
"age": 30
}
6. Unescaped Special Characters
Certain characters inside strings must be escaped with a backslash. These include double quotes, backslashes, and control characters like newlines and tabs.
// โ Invalid - unescaped quote and newline
{
"message": "She said "hello"",
"path": "C:\new\folder"
}
// โ Valid - properly escaped
{
"message": "She said \"hello\"",
"path": "C:\\new\\folder"
}
Common escape sequences: \" (double quote), \\ (backslash), \n (newline), \t (tab), \r (carriage return).
7. Using undefined or NaN
JSON only supports null, not JavaScript's undefined, NaN, or Infinity. These are not valid JSON values.
// โ Invalid
{"value": undefined, "score": NaN}
// โ Valid
{"value": null, "score": 0}
Note: JSON.stringify() in JavaScript automatically removes undefined values and converts NaN to null.
8. Incorrect Nesting (Mismatched Brackets)
Missing or extra brackets and braces will break your JSON. Always ensure they're properly matched.
// โ Invalid - missing closing bracket
{
"users": [
{"name": "Alice"},
{"name": "Bob"}
}
// โ Valid - properly closed
{
"users": [
{"name": "Alice"},
{"name": "Bob"}
]
}
Tip: Use our JSON formatter to instantly spot bracket mismatches. The error message will point to the exact position.
9. Duplicate Keys
While technically allowed by the JSON spec, duplicate keys lead to unpredictable behavior because different parsers handle them differently โ some keep the first value, others keep the last.
// โ ๏ธ Problematic - duplicate "name" key
{
"name": "Alice",
"age": 30,
"name": "Bob"
}
// โ Better - unique keys
{
"name": "Bob",
"age": 30
}
Best practice: Always use unique keys. Most linters and validators will flag duplicates as warnings.
10. Wrong Root Type
A valid JSON document must have a single root value. While the spec allows any value (string, number, array, object, boolean, null) at the root level, many APIs and tools expect specifically an object or array.
// These are ALL valid JSON:
{"name": "Alice"}
[1, 2, 3]
"hello"
42
true
null
// โ This is NOT valid - multiple root values
{"name": "Alice"} {"name": "Bob"}
If you need multiple JSON objects, use an array: [{"name": "Alice"}, {"name": "Bob"}], or use NDJSON (Newline Delimited JSON) format.
How to Prevent JSON Errors
- Always validate: Use our JSON validator before deploying or sending JSON data.
- Use
JSON.stringify(): Generate JSON from objects programmatically instead of writing it by hand. - Enable linting: Configure ESLint or your editor's JSON validator to catch errors as you type.
- Test with real data: Don't just test with simple examples โ use realistic, complex JSON payloads.
- Read the error message: Modern parsers include the line and character position of the error. Use this to locate the problem quickly.
Conclusion
JSON errors are almost always caused by simple syntax mistakes. By knowing the ten most common errors โ trailing commas, wrong quotes, missing commas, unescaped characters, and bracket mismatches โ you can diagnose and fix problems in seconds instead of minutes.
Use our JSON formatter and validator to catch these errors instantly. For more JSON knowledge, explore our guides on working with JSON in JavaScript and JSON Schema validation.