"Unexpected token" errors can waste hours of development time. JSON is strict—one misplaced comma or quote breaks everything.
This guide covers the 15 most common JSON errors with instant fixes you can copy-paste.
How to Diagnose JSON Errors#
Most JSON errors fall into a few categories. Here's how to read error messages:
- "Unexpected token" — Usually a syntax character in the wrong place
- "Unexpected end of JSON" — Missing closing bracket or brace
- "Expected comma or ] after array element" — Missing comma
Best Tools: Use JSON Formatter to validate and see exactly where errors occur.
Error #1: Trailing Commas#
The Problem: JSON doesn't allow trailing commas (unlike JavaScript).
❌ Invalid:
{
"name": "John",
"age": 30, ← Extra comma
}
✅ Fixed:
{
"name": "John",
"age": 30
}
Why it happens: Developers copy JavaScript objects or add/remove properties without updating commas.
Error #2: Single Quotes Instead of Double Quotes#
The Problem: JSON requires double quotes (" ") for strings. Single quotes (' ') are invalid.
❌ Invalid:
{'name': 'John'}
✅ Fixed:
{"name": "John"}
Quick Fix: Search and replace all single quotes with double quotes (be careful with apostrophes in strings!).
Error #3: Unescaped Quotes in Strings#
The Problem: Quotes inside strings must be escaped with backslash.
❌ Invalid:
{"message": "He said "Hello""}
✅ Fixed:
{"message": "He said \"Hello\""}
Escape Sequences:
\"— Double quote\\— Backslash\n— Newline\t— Tab
Error #4: Missing Commas#
The Problem: Each property/array item needs a comma separator.
❌ Invalid:
{
"name": "John"
"age": 30
}
✅ Fixed:
{
"name": "John",
"age": 30
}Error #5: Comments Not Allowed#
The Problem: JSON doesn't support comments (// or /* */).
❌ Invalid:
{
// User data
"name": "John"
}
✅ Options:
- Remove comments entirely
- Use JSON5 or JSONC (if supported)
- Add a "_comment" property:
"_comment": "User data"
Error #6: undefined, NaN, Infinity#
The Problem: JavaScript values undefined, NaN, and Infinity aren't valid JSON.
❌ Invalid:
{
"value": undefined,
"calculation": NaN
}
✅ Fixed:
{
"value": null,
"calculation": null
}
Use null or omit the property entirely.
JSON Debugging Checklist#
When you encounter a JSON error:
- Paste into JSON Formatter — sees exact line
- Check for trailing commas
- Verify all quotes are double quotes
- Look for unescaped special characters
- Count opening/closing braces and brackets
- Remove any comments
- Validate property names are in quotes
Prevention: Use a linter (ESLint, Prettier) and JSON validation in your IDE.
Frequently Asked Questions
Why do I get "Unexpected token" errors?▼
Can I use trailing commas in JSON?▼
How do I escape quotes in JSON strings?▼
Can I add comments to JSON?▼
What is the difference between null and undefined in JSON?▼
Format & Validate Your JSON
Use our JSON formatter to instantly find and fix syntax errors. See exactly where your JSON is invalid.
Related Articles
What is JSON
Learn JSON fundamentals: syntax rules, data types, real-world examples, and best practices. The definitive guide to JavaScript Object Notation.
How to Convert JSON to CSV
Learn how to convert JSON data to CSV format. Step-by-step tutorial covering nested objects, arrays, and best practices for Excel compatibility.