Common JSON Errors and How to Fix Them
Unexpected token < at position 0
JSON.parse: unexpected character at line 1 column 2
SyntaxError: Unexpected end of JSON input
If you've worked with JSON, you've seen these error messages. And they're frustratingly unhelpful.
JSON has strict syntax rules. One wrong character and the entire document fails to parse. Let's go through the most common mistakes and how to fix them.
The 10 Most Common JSON Errors
1. Trailing Commas
The Problem:
{
"name": "Alice",
"age": 30,
}
Why It Fails: JSON does not allow commas after the last element. JavaScript does. This trips up developers constantly.
The Fix:
{
"name": "Alice",
"age": 30
}
Remove the comma after the last value.
2. Single Quotes Instead of Double Quotes
The Problem:
{
'name': 'Alice'
}
Why It Fails: JSON requires double quotes. Single quotes are a JavaScript thing.
The Fix:
{
"name": "Alice"
}
Replace all single quotes with double quotes.
3. Unquoted Keys
The Problem:
{
name: "Alice"
}
Why It Fails: In JavaScript, unquoted keys are valid. In JSON, all keys must be strings (double-quoted).
The Fix:
{
"name": "Alice"
}
Wrap every key in double quotes.
4. Comments in JSON
The Problem:
{
"name": "Alice", // User's name
"age": 30 /* in years */
}
Why It Fails:
JSON does not support comments. Period. No //, no /* */.
The Fix:
{
"name": "Alice",
"age": 30
}
Remove all comments. If you need comments, consider JSONC (JSON with Comments) or YAML for configuration files.
5. Undefined Values
The Problem:
{
"name": "Alice",
"nickname": undefined
}
Why It Fails:
undefined is a JavaScript concept. JSON has null for empty values.
The Fix:
{
"name": "Alice",
"nickname": null
}
Use null instead of undefined.
6. NaN and Infinity
The Problem:
{
"value": NaN,
"max": Infinity
}
Why It Fails:
JSON numbers must be finite. NaN, Infinity, and -Infinity are not valid.
The Fix:
{
"value": null,
"max": 9999999999
}
Use null for undefined numbers, or a large number for maximum values.
7. Invalid Escape Sequences
The Problem:
{
"path": "C:\Users\Alice"
}
Why It Fails:
Backslashes start escape sequences. \U and \A aren't valid escapes.
The Fix:
{
"path": "C:\\Users\\Alice"
}
Double the backslashes, or use forward slashes for paths.
Valid escape sequences: \" \\ \/ \b \f \n \r \t \uXXXX
8. Unescaped Special Characters
The Problem:
{
"quote": "She said "hello""
}
Why It Fails: The inner double quotes break the string.
The Fix:
{
"quote": "She said \"hello\""
}
Escape internal quotes with backslashes.
9. Numbers with Leading Zeros
The Problem:
{
"code": 007
}
Why It Fails: Leading zeros create octal numbers in some languages. JSON forbids them.
The Fix:
{
"code": 7
}
Or if you need the leading zeros:
{
"code": "007"
}
Remove leading zeros or use a string.
10. Missing Brackets or Braces
The Problem:
{
"users": [
{"name": "Alice"},
{"name": "Bob"
]
}
Why It Fails: Missing closing brace on Bob's object.
The Fix:
{
"users": [
{"name": "Alice"},
{"name": "Bob"}
]
}
Match every { with } and every [ with ].
Using Our Validator to Find Errors
Paste your JSON into our JSON validator to:
- Instantly validate syntax
- Pinpoint errors with line and column numbers
- See error descriptions explaining what went wrong
- Auto-format valid JSON for readability
Example Error Output
Input:
{"name": "Alice", "age": 30,}
Validator Output:
Error at line 1, column 27:
Unexpected token '}' after trailing comma
Expected: string, number, object, array, true, false, or null
Found: }
Debugging Tips
Check the Error Position
Most parsers report line and column numbers. Go to that exact position. The error is usually there or one character before.
Validate Incrementally
For complex JSON, build it piece by piece and validate after each addition. Easier to find where things went wrong.
Use a Formatter First
A good JSON formatter will choke on invalid JSON but might point you to the problem area.
Mind the Encoding
Ensure your file is UTF-8 encoded. BOM (Byte Order Mark) at the start of a file can cause parsing failures.
Watch for Invisible Characters
Copy-pasting from Word or rich text editors can introduce invisible Unicode characters. Re-type suspicious sections.
Quick Reference: Valid JSON Values
| Type | Valid | Invalid |
|---|---|---|
| String | "hello" |
'hello', hello |
| Number | 42, 3.14, -5, 1e10 |
NaN, Infinity, 07 |
| Boolean | true, false |
True, TRUE, yes |
| Null | null |
Null, NULL, undefined |
| Array | [1, 2, 3] |
[1, 2, 3,] |
| Object | {"key": "value"} |
{key: "value"} |
When JSON Still Fails
If your JSON looks correct but still fails:
- Check encoding: Ensure UTF-8, no BOM
- Remove invisible characters: Copy to plain text editor and back
- Check for truncation: Is the file complete?
- Validate the source: Is the API returning actual JSON or an error page?
That last one catches people: an API returns HTML error page, code tries to parse it as JSON, error message says "Unexpected token <" (the < from <html>).
FAQ: JSON Error Questions
Why does my JSON work in JavaScript but not when parsed?
JavaScript object literals are more forgiving than JSON. Single quotes, unquoted keys, trailing commas, comments — all valid in JavaScript, all invalid in JSON. JSON is a strict subset.
How do I fix "Unexpected token < at position 0"?
The data you're trying to parse isn't JSON — it's HTML. This usually means an API returned an error page instead of JSON data. Check your API endpoint and authentication.
Can I use comments in JSON?
No. Standard JSON doesn't support comments. For configuration files that need comments, consider: (1) JSONC (JSON with Comments) in supported tools, (2) YAML, (3) Stripping comments in a preprocessing step.
How do I escape special characters in JSON strings?
Use backslash escapes: \" for quotes, \\ for backslashes, \n for newlines, \t for tabs. For Unicode, use \uXXXX where XXXX is the hex code point.
What's the difference between null and undefined in JSON?
JSON has null but not undefined. In JavaScript-to-JSON conversion, undefined values are omitted. Use null explicitly when you need to represent "no value."
Validate your JSON: JSON Validator →
Format and beautify: JSON Formatter →
Need to learn JSON basics? Check our guide: What is JSON?