Common JSON Errors & How to Fix Them
JSON parsing errors can be frustrating, especially when the error messages are cryptic. This guide covers the most common JSON errors, explains why they happen, and shows you exactly how to fix them.
1. Trailing Commas
One of the most frequent JSON errors. Unlike JavaScript, JSON does not allow trailing commas after the last element in an object or array.
❌ Invalid
{
"name": "John",
"age": 30,
"city": "New York",
}
✅ Fixed
{
"name": "John",
"age": 30,
"city": "New York"
}
2. Single Quotes Instead of Double Quotes
JSON strictly requires double quotes for strings and keys. Single quotes are not valid.
❌ Invalid
{
'name': 'John Doe',
'age': 30
}
✅ Fixed
{
"name": "John Doe",
"age": 30
}
3. Unquoted Property Names
All property names (keys) in JSON must be enclosed in double quotes. This is different from JavaScript objects where unquoted keys are allowed.
❌ Invalid
{
name: "John",
age: 30
}
✅ Fixed
{
"name": "John",
"age": 30
}
4. Comments in JSON
Standard JSON does not support comments. This is a deliberate design decision by Douglas Crockford to keep the format simple and interoperable.
❌ Invalid
{
"name": "John", // user's name
"age": 30 /* user's age */
}
✅ Fixed
{
"name": "John",
"age": 30
}
Advertisement
5. Missing or Extra Brackets
Mismatched curly braces {} or square brackets [] are a common source of errors,
especially in large JSON files.
❌ Invalid — Missing closing bracket
{
"users": [
{"name": "John"},
{"name": "Jane"}
}
✅ Fixed
{
"users": [
{"name": "John"},
{"name": "Jane"}
]
}
6. Incorrect Use of Escape Characters
Special characters in strings must be properly escaped with a backslash.
❌ Invalid — Unescaped characters
{
"path": "C:\new\folder\test",
"message": "He said "hello""
}
✅ Fixed
{
"path": "C:\\new\\folder\\test",
"message": "He said \"hello\""
}
Valid Escape Sequences
| Sequence | Character |
|---|---|
\" | Double quote |
\\ | Backslash |
\/ | Forward slash |
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Carriage return |
\t | Tab |
\uXXXX | Unicode character |
7. Using undefined or NaN
JSON does not support undefined, NaN, Infinity, or -Infinity.
These are JavaScript-specific values.
❌ Invalid
{
"value": undefined,
"result": NaN,
"limit": Infinity
}
✅ Fixed
{
"value": null,
"result": null,
"limit": null
}
8. Duplicate Keys
While technically not a syntax error (most parsers accept it), duplicate keys lead to unpredictable behavior as different parsers handle them differently.
⚠️ Problematic
{
"name": "John",
"age": 30,
"name": "Jane"
}
✅ Fixed
{
"name": "Jane",
"age": 30
}
9. Leading Zeros in Numbers
JSON does not allow leading zeros in numbers. The only exception is 0 itself
and numbers like 0.5.
❌ Invalid
{
"zipCode": 01234,
"price": 007.99
}
✅ Fixed
{
"zipCode": "01234",
"price": 7.99
}
Quick Debugging Checklist
- Check for trailing commas after the last element
- Ensure all strings use double quotes
- Verify all keys are quoted
- Remove any comments
- Match all opening and closing brackets
- Escape special characters properly
- Replace
undefined,NaN, andInfinitywithnull - Check for duplicate keys
- Remove leading zeros from numbers