Common JSON Errors and How to Fix Them
Common JSON Errors
JSON's strict syntax rules mean that even a single misplaced character can break an entire document. Understanding the most common JSON errors will help you debug faster and avoid repeating the same mistakes. Below are the most frequent issues developers encounter, with clear explanations and fixes for each.
1. Trailing Commas
Error: Unexpected token at end of object or array
Trailing commas are permitted in JavaScript objects and arrays but are strictly forbidden in JSON.
Invalid JSON:
{
"name": "Alice",
"age": 30,
}
Fix: Remove the comma after the last item.
{
"name": "Alice",
"age": 30
}
Arrays with trailing commas:
// Invalid
[1, 2, 3,]
// Valid
[1, 2, 3]
2. Missing or Incorrect Quotes Around Keys
Error: Unexpected identifier or Expected double-quoted property name
JSON requires all object keys to be wrapped in double quotes. Single quotes and unquoted keys are not valid, even though they work in JavaScript.
Invalid JSON:
{
name: "Alice",
'occupation': "Engineer"
}
Fix: Always wrap keys in double quotes.
{
"name": "Alice",
"occupation": "Engineer"
}
3. Using Single Quotes for String Values
Error: Unexpected identifier or Unterminated string
Just like keys, string values must use double quotes in JSON. Single quotes are not valid.
Invalid JSON:
{ "greeting": 'Hello, world!' }
Fix: Replace single quotes with double quotes.
{ "greeting": "Hello, world!" }
Tip: If your string contains double quotes, escape them with a backslash: "He said, \"Hello\"".
4. Unescaped Special Characters
Error: Unexpected token or Invalid character
Certain characters must be escaped in JSON strings: double quotes ("), backslashes (\), and control characters.
Invalid JSON:
{
"path": "C:\Users\Documents\file.json",
"quote": "He said "hello""
}
Fix: Escape backslashes and double quotes properly.
{
"path": "C:\\Users\\Documents\\file.json",
"quote": "He said \"hello\""
}
Complete List of JSON Escape Sequences
| Sequence | Represents |
|----------|-----------|
| \" | Double quote |
| \\ | Backslash |
| \/ | Forward slash |
| \b | Backspace |
| \f | Form feed |
| \n | Newline |
| \r | Carriage return |
| \t | Tab |
| \uXXXX | Unicode character (e.g., é for e) |
5. Invalid Numeric Formats
Error: Unexpected number or Invalid number
JSON has specific rules for how numbers can be formatted:
- Leading zeros are not allowed (
01is invalid) - A single leading zero is allowed only for the number
0 - Numbers cannot have trailing dots (
10.is invalid) - Hexadecimal, octal, and binary formats are not supported
Invalid JSON:
{
"invalid1": 01,
"invalid2": 10.,
"invalid3": 0xFF,
"invalid4": 1,000
}
Fix:
{
"valid1": 1,
"valid2": 10.0,
"valid3": 255,
"valid4": 1000
}
6. Invalid Values: undefined, NaN, Infinity
Error: Unexpected token for undefined, NaN, Infinity
JavaScript has values that have no JSON equivalent. These will cause parsing errors:
Invalid JSON:
{
"undefinedVal": undefined,
"notANumber": NaN,
"infinity": Infinity
}
Fix: Replace with valid JSON values or use a convention.
{
"undefinedVal": null,
"notANumber": null,
"infinity": 1.7976931348623157e+308
}
7. Extra Commas or Missing Commas
Error: Expected comma or Unexpected token
Commas in JSON must separate every key/value pair and array element, but must not appear after the last item.
Missing comma (invalid):
{
"name": "Alice"
"age": 30
}
Fix: Add the missing comma.
{
"name": "Alice",
"age": 30
}
8. Deeply Nested Structure Issues
Error: Maximum call stack size exceeded or parser timeout
While not a syntax error, deeply nested JSON (10+ levels) can cause performance issues and make debugging extremely difficult.
Problematic structure:
{
"level1": {
"level2": {
"level3": {
"level4": {
"level5": {
"level6": {
"value": "too deep"
}
}
}
}
}
}
}
Better approach: Flatten the structure or use references.
{
"users": {
"alice": { "role": "admin" }
},
"permissions": {
"admin": ["read", "write", "delete"]
}
}
9. Missing Opening or Closing Braces
Error: Expected ',' or '}' or Unexpected end of JSON input
Unmatched braces and brackets are a common source of JSON errors, especially in large or hand-written JSON files.
Invalid JSON:
{
"name": "Alice",
"items": [1, 2, 3
}
Fix: Ensure every { has a matching }, and every [ has a matching ].
{
"name": "Alice",
"items": [1, 2, 3]
}
Pro tip: Indent your JSON properly to visually match opening and closing brackets. A JSON formatter can do this automatically.
How to Debug JSON Errors Effectively
- Use a JSON validator -- Paste your JSON into a free JSON validator to get the exact line and column of the first error.
- Validate incrementally -- For large files, validate one section at a time to isolate the error.
- Watch for copy-paste artifacts -- Invisible characters, smart quotes, and non-breaking spaces often sneak in from word processors or web pages.
- Check character encoding -- Ensure your file is saved as UTF-8. Other encodings can produce invisible errors.
- Use a linter -- Tools like ESLint with the
jsonplugin catch JSON errors during development.
Remember: JSON parsers typically report only the first syntax error. Fix that one, re-validate, and repeat until the document passes. Our JSON Formatter & Validator tool gives you instant, detailed error messages to speed up this process.