Common JSON Mistakes and How to Avoid Them
Common JSON Mistakes and How to Avoid Them
Even experienced developers make JSON syntax errors. Here are the 20 most common mistakes, organized by category, with examples showing what's wrong and how to fix it.
Syntax Errors
1. Trailing Commas π΄ Very Common
Wrong:
{
"name": "Alice",
"age": 30,
}
Correct:
{
"name": "Alice",
"age": 30
}
JSON doesn't allow trailing commas. This is the #1 error we seeβlikely because JavaScript allows them.
π‘ Pro tip: When reordering properties, always check the last line.
2. Single Quotes π΄ Very Common
Wrong:
{
'name': 'Alice'
}
Correct:
{
"name": "Alice"
}
JSON requires double quotes. Single quotes are never valid, even though JavaScript accepts them.
π‘ Pro tip: If copying from Python code, watch for single quotesβPython doesn't distinguish.
3. Unquoted Keys π΄ Very Common
Wrong:
{
name: "Alice"
}
Correct:
{
"name": "Alice"
}
All keys must be quoted strings. JavaScript object shorthand doesn't work in JSON.
4. Comments π‘ Common
Wrong:
{
"name": "Alice", // user's name
"age": 30 /* in years */
}
Correct:
{
"name": "Alice",
"age": 30
}
Standard JSON has no comment syntax. If you need comments, consider:
- JSONC (JSON with Comments) β supported by VS Code
- JSON5 β superset with relaxed syntax
- A separate documentation file
Read our full guide: JSON Comments: Why They Don't Exist and Workarounds
5. Missing Commas π‘ Common
Wrong:
{
"name": "Alice"
"age": 30
}
Correct:
{
"name": "Alice",
"age": 30
}
Every property except the last needs a trailing comma. JSONLint points to the exact line.
Data Type Errors
6. Using undefined π‘ Common
Wrong:
{
"name": "Alice",
"nickname": undefined
}
Correct:
{
"name": "Alice",
"nickname": null
}
undefined is a JavaScript concept, not valid JSON. Use null for missing/empty values.
7. NaN and Infinity π’ Rare
Wrong:
{
"result": NaN,
"limit": Infinity
}
Correct:
{
"result": null,
"limit": null
}
JSON numbers must be finite. Represent these as null or use a string like "NaN" with application-level handling.
8. Wrong Boolean/Null Case π‘ Common
Wrong:
{
"active": True,
"deleted": FALSE,
"data": NULL
}
Correct:
{
"active": true,
"deleted": false,
"data": null
}
Booleans and null must be lowercase. Python developers often make this mistake.
9. Leading Zeros in Numbers π’ Rare
Wrong:
{
"code": 007,
"zip": 02134
}
Correct:
{
"code": 7,
"zip": "02134"
}
Leading zeros make numbers invalid (they look like octal). If you need them, use a string.
10. Numeric Precision Issues π’ Rare
Wrong (may lose precision):
{
"bigId": 9007199254740993
}
Safer:
{
"bigId": "9007199254740993"
}
JavaScript (and JSON parsers in many languages) can't precisely represent integers larger than 2^53. Use strings for big IDs.
11. Hexadecimal Numbers π’ Rare
Wrong:
{
"color": 0xFF5733
}
Correct:
{
"color": "#FF5733"
}
JSON only supports decimal numbers. Use strings for hex values.
String Errors
12. Unescaped Special Characters π‘ Common
Wrong:
{
"path": "C:\Users\Alice"
}
Correct:
{
"path": "C:\\Users\\Alice"
}
Backslashes must be escaped. This also applies to quotes inside strings: "She said \"hello\"".
13. Multi-line Strings π‘ Common
Wrong:
{
"message": "Hello
World"
}
Correct:
{
"message": "Hello\nWorld"
}
Strings can't contain literal newlines. Use \n for line breaks.
14. Tab Characters π’ Rare
Wrong:
{
"data": "col1 col2"
}
Correct:
{
"data": "col1\tcol2"
}
Literal tabs must be escaped as \t. Most editors hide this issue.
15. Control Characters π’ Rare
Wrong:
{
"text": "Hello\u0000World"
}
Many control characters (ASCII 0-31) are invalid even when escaped. The null character (\u0000) is particularly problematic.
π‘ Pro tip: If processing user input, sanitize control characters before JSON encoding.
16. Invalid Unicode Escapes π’ Rare
Wrong:
{
"emoji": "\u123"
}
Correct:
{
"emoji": "\u0123"
}
Unicode escapes must be exactly 4 hex digits: \uXXXX.
Structural Errors
17. Duplicate Keys π‘ Common
Problematic:
{
"name": "Alice",
"name": "Bob"
}
This is technically valid JSON (the spec allows it), but behavior varies by parser. Most take the last value, but don't rely on this.
π‘ Pro tip: JSONLint warns about duplicates. Always fix them.
18. Root Must Be Object or Array π’ Rare
Wrong:
"just a string"
Correct:
{
"value": "just a string"
}
The root element must be an object {} or array []. Primitives alone aren't valid JSON documents (though some parsers accept them).
19. Empty Key π’ Rare
Technically valid but problematic:
{
"": "empty key"
}
An empty string is a valid key, but many systems don't handle it well. Avoid if possible.
20. Circular References (Conceptual) π‘ Common
Impossible to represent:
const obj = { name: "Alice" };
obj.self = obj; // circular!
JSON.stringify(obj); // TypeError
JSON can't represent circular structures. Flatten your data or use a library that handles cycles.
Quick Reference
| Error | Frequency | Quick Fix |
|---|---|---|
| Trailing comma | π΄ Very Common | Remove comma after last item |
| Single quotes | π΄ Very Common | Replace with double quotes |
| Unquoted keys | π΄ Very Common | Add quotes around keys |
| Comments | π‘ Common | Remove or use JSONC |
| Missing commas | π‘ Common | Add comma between items |
undefined |
π‘ Common | Use null instead |
| Wrong case (True/NULL) | π‘ Common | Use lowercase |
| Unescaped backslash | π‘ Common | Double the backslash |
| Multi-line string | π‘ Common | Use \n |
| Duplicate keys | π‘ Common | Remove duplicates |
How JSONLint Helps
JSONLint catches all these errors and shows you exactly where they are:
- Line numbers β Jump directly to the problem
- Clear messages β Understand what's wrong
- Auto-fix β The "Try Fix" button handles common issues like trailing commas
Validate your JSON before deploying. A syntax error in a config file can take down your whole app.
Preventing Errors
- Use an editor with JSON support β VS Code, Sublime, and others highlight errors as you type
- Let your language generate JSON β
JSON.stringify()in JavaScript,json.dumps()in Python - Validate in CI/CD β Catch errors before they reach production
- Format consistently β Prettier and similar tools enforce consistent style
Most JSON errors are simple typos. Use tools to catch them early.
Related Resources
- JSON Validator β Validate your JSON and find errors instantly
- JSON Minify β Compress JSON by removing whitespace
- JSON Comments Guide β Learn about JSON comment alternatives
- Fix "Unexpected End of JSON Input" β Debug parsing errors
- JSON Unescape β Fix escaped string issues
- Mastering JSON Format β Complete JSON syntax reference
Related Articles
Understanding the Benefits of Using a JSON Beautifier
Learn why formatting and beautifying JSON improves code readability, debugging, and collaboration.
How to Fix 'Unexpected End of JSON Input' Error
Learn what causes the 'Unexpected end of JSON input' error and how to fix it with practical solutions for JavaScript, Python, and other languages.
How to Open JSON Files
A complete guide to opening and viewing JSON files on any operating system using various methods and tools.
JSON Comments: Why They Don't Exist and How to Work Around It
Learn why JSON doesn't support comments, explore workarounds like JSONC and JSON5, and discover best practices for documenting your JSON files.