How to Fix 'Unexpected Token' JSON Errors: A Complete Guide
Struggling with 'Unexpected Token' errors in JSON? Learn the root causes, common mistakes, and how to fix them instantly with our Smart Repair tool.
The Problem: You're parsing a JSON response from an API, loading a config file, or debugging a web application—and suddenly you see: SyntaxError: Unexpected token.
This cryptic error message is one of the most common frustrations developers face when working with JSON. But what does it actually mean, and how do you fix it?
This guide will show you the root causes of "Unexpected Token" errors and how to resolve them instantly.
What Does "Unexpected Token" Mean?
In JSON parsing, a "token" is any meaningful character or sequence: {, }, [, ], :, ,, or a string/number value.
When the parser encounters a character it doesn't expect based on JSON's strict syntax rules, it throws an "Unexpected Token" error.
Example:
{
"name": "John",
"age": 30,
}The trailing comma after 30 is an unexpected token because JSON doesn't allow trailing commas.
The 7 Most Common Causes
1. Trailing Commas
The Error:
{
"user": "admin",
"role": "developer",
}Why It Fails: JSON forbids commas after the last item in an object or array.
The Fix:
{
"user": "admin",
"role": "developer"
}Auto-Fix Trailing Commas
Our Smart Repair tool automatically detects and removes trailing commas in your JSON.
Try Smart Repair →2. Single Quotes Instead of Double Quotes
The Error:
{
'name': 'Alice'
}Why It Fails: JSON requires double quotes (") for all strings and keys. Single quotes are not valid.
The Fix:
{
"name": "Alice"
}3. Unquoted Keys
The Error:
{
name: "Bob"
}Why It Fails: All object keys must be strings enclosed in double quotes.
The Fix:
{
"name": "Bob"
}4. Comments in JSON
The Error:
{
// This is a user object
"name": "Charlie"
}Why It Fails: JSON does not support comments. Any // or /* */ will cause a parsing error.
The Fix: Remove all comments or use a format like JSON5 or JSONC if your environment supports it.
{
"name": "Charlie"
}5. Missing Commas Between Items
The Error:
{
"name": "David"
"age": 25
}Why It Fails: Each key-value pair must be separated by a comma.
The Fix:
{
"name": "David",
"age": 25
}6. Extra Commas
The Error:
{
"name": "Eve",,
"age": 28
}Why It Fails: Double commas are invalid syntax.
The Fix:
{
"name": "Eve",
"age": 28
}7. Invisible Characters (BOM, Zero-Width Spaces)
The Error: Your JSON looks perfect, but it still fails to parse.
Why It Fails: Hidden characters like Byte Order Marks (BOM) or zero-width spaces can break JSON parsing.
The Fix: Use a text editor that shows invisible characters, or use our JSON Formatter to detect and remove them automatically.
How to Debug "Unexpected Token" Errors
Step 1: Identify the Line Number
Most parsers will tell you where the error occurred:
SyntaxError: Unexpected token } in JSON at position 42
Use the position or line number to locate the problem area.
Step 2: Check for Common Mistakes
- Trailing commas
- Single quotes
- Unquoted keys
- Missing commas
Step 3: Use a Validator
Paste your JSON into our Online JSON Formatter to get instant, line-by-line error detection.
Preventing "Unexpected Token" Errors
1. Use a Linter
Integrate a JSON linter into your IDE (VS Code, Sublime Text) to catch errors as you type.
2. Validate Before Deployment
Always validate JSON config files before deploying to production. A single syntax error can bring down an entire service.
3. Use Schema Validation
Define a JSON Schema to enforce structure and catch errors early.
4. Automate Repairs
Use tools like our Smart Repair to automatically fix common syntax errors before they cause issues.
Real-World Example: API Response Debugging
Scenario: You're fetching user data from an API, and the response fails to parse.
Error:
SyntaxError: Unexpected token < in JSON at position 0
Cause: The server returned HTML (e.g., an error page) instead of JSON.
Solution: Check the HTTP status code and Content-Type header before parsing:
fetch('/api/users')
.then(response => {
if (!response.ok) {
throw new Error(`HTTP ${response.status}`)
}
if (!response.headers.get('content-type')?.includes('application/json')) {
throw new Error('Response is not JSON')
}
return response.json()
})
.then(data => console.log(data))
.catch(error => console.error('Parse error:', error))Conclusion
"Unexpected Token" errors are frustrating, but they're almost always caused by one of seven common mistakes. By understanding JSON's strict syntax rules and using validation tools, you can catch and fix these errors in seconds.
Key Takeaways:
- No trailing commas
- Always use double quotes
- Quote all object keys
- No comments in JSON
- Validate before deployment
- Use automated repair tools
Fix JSON Errors Instantly
Paste your broken JSON into our Smart Repair tool and watch it get fixed automatically. No manual debugging required.
Launch Editor →