Common JSON Errors and How to Fix Them
JSON is simple by design, but its strict syntax catches many developers off guard. This guide covers the most frequent errors and exactly how to fix them.
1. Trailing Comma
The most common JSON error. Unlike JavaScript object literals, JSON forbids trailing commas after the last element.
Wrong
{
"name": "Alice",
"age": 30,
} Correct
{
"name": "Alice",
"age": 30
} Fix: Remove the comma after the last property.
2. Comments in JSON
JSON does not support comments of any kind. No //, no /* */, no #.
Wrong
{
"name": "Alice", // comment
"age": 30
} Correct
{
"name": "Alice",
"age": 30
} Fix: Remove all comments. Use a separate documentation file or a "_comment" key if needed.
3. Single Quotes
JSON strings must use double quotes. Single quotes cause a parse error.
Wrong
{
'name': 'Alice'
} Correct
{
"name": "Alice"
} Fix: Replace all single quotes with double quotes.
4. Missing Commas Between Properties
Every property in an object (except the last) must be separated by a comma.
Wrong
{
"name": "Alice"
"age": 30
} Correct
{
"name": "Alice",
"age": 30
} Fix: Add commas between properties.
5. Unquoted Keys
Object property names must be double-quoted strings in JSON.
Wrong
{
name: "Alice"
} Correct
{
"name": "Alice"
} Fix: Wrap all keys in double quotes.
6. Undefined Values
JavaScript undefined is not a valid JSON value. Use null instead.
Wrong
{
"name": "Alice",
"nickname": undefined
} Correct
{
"name": "Alice",
"nickname": null
} Fix: Replace undefined with null.
7. Numbers with Leading Zeros
Numbers cannot have leading zeros (except for 0 itself and 0.x decimal values).
Wrong
{
"code": 007
} Correct
{
"code": 7
} Fix: Remove leading zeros from numbers.
8. Unescaped Characters in Strings
Certain characters must be escaped in JSON strings: double quotes, backslashes, and control characters.
Wrong
{"path": "C:\Users\Alice"} Correct
{"path": "C:\\Users\\Alice"} Fix: Escape special characters with a backslash.
9. BOM (Byte Order Mark)
Some text editors add an invisible BOM character at the start of files. This causes JSON parse errors.
Fix: Save the file without BOM. In most editors, this is a save option (UTF-8 without BOM).
10. Using the Wrong Data Type
JSON has specific types: string, number, boolean, null, array, and object. Using other JavaScript types causes errors.
Wrong
{
"created": new Date(),
"regex": /pattern/
} Correct
{
"created": "2025-01-15T10:30:00Z",
"regex": "pattern"
} Fix: Convert non-JSON types to strings or numbers before serializing.
How to Catch These Errors
Use the JSON Validator to check your JSON before using it. It provides clear error messages that point to the exact location of the problem.
- Paste your JSON into the validator.
- If it is invalid, read the error message carefully.
- The error position usually indicates which line and which character caused the issue.
- Fix that specific issue and validate again.