How to Format and Validate JSON: Common Errors and How to Fix Them
Why JSON Errors Are So Common
JSON looks simple — it's basically a stricter version of JavaScript objects. But its strict syntax catches out even experienced developers, especially when:
- Manually editing JSON configuration files
- Copying JSON from an API response that arrived malformed
- Building JSON strings through string concatenation in code
- Converting data from another format (CSV, XML) to JSON
- Working with JSON from a third-party service that occasionally sends invalid data
The Most Common JSON Errors
1. Trailing Comma
This is the single most common JSON error. JavaScript objects and arrays allow trailing commas, but JSON does not:
// INVALID JSON
{
"name": "Alex",
"role": "admin", ← trailing comma
}
// VALID JSON
{
"name": "Alex",
"role": "admin" ← no trailing comma
}
2. Unquoted Keys
JavaScript allows unquoted keys in objects: {name: "Alex"}. JSON requires all keys to be double-quoted strings:
// INVALID JSON
{name: "Alex"}
// VALID JSON
{"name": "Alex"}
3. Single Quotes Instead of Double Quotes
JSON requires double quotes for both keys and string values. Single quotes are not valid:
// INVALID JSON
{'name': 'Alex'}
// VALID JSON
{"name": "Alex"}
4. Unescaped Special Characters in Strings
Certain characters must be escaped within JSON strings:
| Character | Escaped Form |
|---|---|
| Double quote " | \" |
| Backslash \ | \ |
| Newline |
|
| Tab | |
| Carriage return |
|
5. Comments in JSON
JSON does not support comments. A common mistake when editing configuration files:
// INVALID JSON
{
// Database configuration
"host": "localhost",
"port": 5432 /* PostgreSQL default port */
}
// If you need comments in config files, use YAML or JSONC (VS Code supports .jsonc)
6. Numeric Values as Strings
JSON distinguishes between the number 42 and the string "42". A common error from manual editing:
// May cause type errors in your application
{"count": "42"} ← string, not a number
// Correct: unquoted numeric values
{"count": 42} ← number
7. null, true, false Must Be Lowercase
JSON boolean and null literals are case-sensitive and must be lowercase:
// INVALID
{"active": True, "data": NULL}
// VALID
{"active": true, "data": null}
How to Debug JSON Errors Efficiently
- Use a formatter first — Paste your JSON into Flixfer's JSON Formatter. It will immediately show you the exact line and character position of any syntax error, with a human-readable description.
- Binary search for the error — For very long JSON (10,000+ lines), if the error message says "line 4,521", remove the bottom half of the document and try again. This narrows down the problem location quickly.
- Check for invisible characters — Copy-pasted JSON sometimes contains Unicode non-breaking spaces, zero-width spaces, or BOM (Byte Order Mark) characters that are invisible but break parsing. The formatter's character-level error highlighting catches these.
- Validate against a schema — If the JSON is syntactically valid but your application still rejects it, the structure may not match the expected schema. Check the API documentation or schema definition for required fields and data types.
Developer Workflow Recommendation
Never manually edit production JSON files without a formatter. The time investment of pasting into a formatter is always less than the time spent debugging a silent JSON parsing failure that causes a runtime error three levels up the call stack.