Common JSON Errors and How to Fix Them
Even experienced developers make JSON syntax errors. Here are the most common mistakes and how to fix them.
1. Trailing Commas
One of the most frequent errors:
Wrong:
{
"name": "John",
"age": 30,
}
The comma after "30" is invalid.
Fixed:
{
"name": "John",
"age": 30
}
Remove the trailing comma after the last element.
2. Single Quotes Instead of Double Quotes
JSON requires double quotes:
Wrong:
{'name': 'John'}
Fixed:
{"name": "John"}
Always use double quotes for strings and keys.
3. Unquoted Keys
All keys must be quoted strings:
Wrong:
{name: "John"}
Fixed:
{"name": "John"}
Unlike JavaScript objects, JSON requires quoted keys.
4. Missing Quotes Around Strings
String values need quotes:
Wrong:
{"name": John}
Fixed:
{"name": "John"}
5. Comments in JSON
JSON doesn't support comments:
Wrong:
{
"name": "John" // user's name
}
Fixed:
{
"name": "John"
}
Remove all comments or use a preprocessor that strips them.
6. Using undefined
JSON doesn't have undefined:
Wrong:
{"value": undefined}
Fixed:
{"value": null}
Use null for empty values.
7. NaN and Infinity
JSON doesn't support these special numbers:
Wrong:
{"value": NaN}
Fixed:
{"value": null}
// or
{"value": "NaN"}
Use null or a string representation.
8. Incorrect Escaping
Backslashes need to be escaped:
Wrong:
{"path": "C:\Users\John"}
The backslashes aren't properly escaped.
Fixed:
{"path": "C:\\Users\\John"}
Escape backslashes with another backslash.
9. Multiline Strings
JSON strings can't span multiple lines:
Wrong:
{"message": "Line 1
Line 2"}
Fixed:
{"message": "Line 1\nLine 2"}
Use escape sequences for newlines.
10. Missing Commas
Forgetting commas between elements:
Wrong:
{
"name": "John"
"age": 30
}
Fixed:
{
"name": "John",
"age": 30
}
Add commas between all elements except the last.
11. Extra Commas in Arrays
Wrong:
{"numbers": [1, 2, , 4]}
Fixed:
{"numbers": [1, 2, 4]}
Remove empty positions in arrays.
12. Mismatched Brackets
Wrong:
{"items": [1, 2, 3}
Fixed:
{"items": [1, 2, 3]}
Ensure all brackets and braces are properly paired.
Debugging Tips
Use a JSON Validator
Tools like JSONSpark can instantly identify syntax errors and show you exactly where they occur.
Format Your JSON
Formatting makes errors easier to spot. Minified JSON is harder to debug.
Check Line by Line
When you get a parsing error with a line number, check that line and the one before it.
Look for Encoding Issues
Non-UTF-8 characters or BOM markers can cause parsing failures.
Conclusion
Most JSON errors come from a handful of common mistakes. Use JSONSpark to validate your JSON and catch these errors instantly.