JSON is the backbone of modern APIs, config files, and data exchange. When it breaks, error messages are often cryptic. This guide covers the most common JSON errors, how to spot them, and how to fix them fast.
JSON Syntax Rules
JSON is strict about its syntax. Unlike JavaScript objects, JSON has no room for flexibility. Here are the rules:
- Keys must be double-quoted strings.
"name"is valid,nameor'name'is not. - Strings must use double quotes.
"hello"works,'hello'does not. - No trailing commas. The last item in an array or object must not have a comma after it.
- No comments. JSON does not support
//or/* */comments. - Values can be: strings, numbers, booleans (
true/false),null, arrays, or objects.
The 7 Most Common JSON Errors
1. Trailing Commas
The most frequent mistake. JavaScript allows trailing commas, JSON does not:
{
"name": "Alice",
"age": 30, ← trailing comma
}{
"name": "Alice",
"age": 30
}2. Single Quotes
JSON requires double quotes for all strings and keys. This is a common mistake when copying from JavaScript or Python code:
{'name': 'Alice'} ← single quotes{"name": "Alice"} ← double quotes3. Unquoted Keys
JavaScript objects allow unquoted keys. JSON does not:
{name: "Alice"} ← unquoted key{"name": "Alice"} ← quoted key4. Comments
JSON has no comment syntax. If you need comments in config files, consider JSONC (JSON with Comments, used by VS Code) or YAML:
{
// database config
"host": "localhost",
"port": 5432 /* default postgres port */
}A common workaround is to use a "_comment" key, though this adds to the payload:
{
"_comment": "database config",
"host": "localhost",
"port": 5432
}5. Missing or Extra Brackets
Mismatched {} or [] brackets are hard to spot in deeply nested structures. Most editors highlight matching brackets, and a JSON formatter will immediately show where the mismatch is.
{
"users": [
{"name": "Alice"},
{"name": "Bob"}
}6. Invalid Number Formats
JSON numbers have specific rules:
.5 ← must have leading zero: 0.5
+1 ← no leading plus sign
0x1A ← no hex notation
NaN ← not a valid JSON value
Infinity ← not a valid JSON value7. Unescaped Special Characters
Strings containing special characters need proper escaping:
" → \" (double quote)
\ → \\ (backslash)
newline → \n
tab → \tA common source of this error is file paths on Windows:
{"path": "C:\Users\data"}{"path": "C:\\Users\\data"}How to Debug JSON Errors
- 1Use a JSON formatter. Paste your JSON into a JSON Formatter to get an immediate error message with the line number.
- 2Check the error position.
JSON.parse()errors include a position number.Unexpected token at position 142means the error is at character 142 of the string. - 3Look at the character before the error. If the parser says "unexpected token at line 5," the actual problem is often on line 4 (a missing comma, for example).
- 4Validate programmatically. Wrap
JSON.parse()in a try/catch to handle malformed input gracefully.
function parseJSON(input) {
try {
return { data: JSON.parse(input), error: null };
} catch (e) {
return { data: null, error: e.message };
}
}JSON vs JavaScript Objects
Many errors come from confusing JSON with JavaScript object literals. Here's a quick comparison:
| Feature | JSON | JavaScript |
|---|---|---|
| Keys | Must be double-quoted | Can be unquoted |
| Strings | Double quotes only | Single, double, or backticks |
| Trailing commas | Not allowed | Allowed |
| Comments | Not allowed | Allowed |
| Functions | Not allowed | Allowed as values |
undefined | Not allowed | Allowed |
Building APIs that serve JSON?
DigitalOcean App Platform deploys Node.js, Python, and Go apps from a Git repo with zero server config. Auto-scaling, free SSL, and built-in monitoring included.
Validate Your JSON
Use our JSON Formatter & Validator to instantly check and fix your JSON. It pinpoints errors, formats the output with proper indentation, and runs entirely in your browser. For schema validation, try the JSON Schema Validator to verify your JSON matches an expected structure.