The "Unexpected token < in JSON" error is one of the most frustrating JSON errors. It occurs when you're expecting JSON data, but instead receive HTML (usually an error page).
The < character is the start of HTML tags, which means your API or server returned an HTML error page instead of JSON. Learn how to fix this instantly using our free JSON Fixer tool.
What Does "Unexpected token < in JSON" Mean?
This error means JavaScript's JSON.parse() encountered an HTML tag (starting with <) when it expected JSON. This typically happens when:
- API endpoint returns an HTML error page (404, 500, etc.)
- Server redirects to a login page
- API endpoint doesn't exist
- Content-Type header is wrong (text/html instead of application/json)
- Network error returns HTML error page
Common Causes
1. API Endpoint Doesn't Exist (404 Error)
The server returns an HTML 404 page instead of JSON when the endpoint is wrong.
<html> <head><title>404 Not Found</title></head> <body>Page not found</body> </html>
2. Server Error (500 Error)
Server crashes return HTML error pages instead of JSON responses.
3. Authentication Required
API requires authentication and redirects to a login HTML page.
How to Fix "Unexpected token < in JSON"
Check the API Response
Before parsing, check what you actually received. Log the response to see if it's HTML or JSON.
fetch('/api/users')
.then(response => {
const contentType = response.headers.get('content-type');
if (!contentType.includes('application/json')) {
throw new Error('Response is not JSON');
}
return response.json();
});Verify API Endpoint
Ensure the API endpoint URL is correct. Test it in a browser or use curl to see what it returns.
Check Response Status
Always check HTTP status codes. Only parse JSON if status is 200-299.
fetch('/api/users')
.then(response => {
if (!response.ok) {
throw new Error('API error: ' + response.status);
}
return response.json();
});Fix JSON Errors Instantly — Try It Below
Paste your JSON and our tool will auto-repair syntax errors in seconds.
Advanced JSON Fixer & Recovery Engine
Paste → Instant fix. Repair malformed JSON: trailing commas, missing quotes, broken arrays, AI-generated JSON — 100% client-side
Repair options
Fix from API error
Paste an error like "Unexpected token } in JSON at position 245" to highlight the position.
JSON Input
Fixed JSON Output
JSON Fixer Features
Automatic Error Detection
Scans and identifies all JSON syntax errors automatically
Smart JSON Repair
Fixes common errors like trailing commas, single quotes, and unquoted keys
Precise Error Location
Shows exact line and column numbers for each error
Visual Error Highlighting
Highlights problematic lines with color-coded indicators
Learn More About JSON Fixing
10 Most Common JSON Mistakes Developers Make
Learn about the most common JSON mistakes and how to fix them instantly with examples.
Read Guide →25 Broken JSON Examples and How to Fix Them
Real-world broken JSON examples with step-by-step fixes and explanations.
Read Guide →Why JSON Breaks in Real-World APIs
Understand why APIs return broken JSON in production and how to fix it effectively.
Read Guide →How JSON Fixers Work Internally
Learn how JSON fixers work internally and why manual fixing often fails.
Read Guide →FAQ
Why am I getting HTML instead of JSON?
Usually because the API endpoint is wrong, the server returned an error page, or authentication is required. Check the API URL and response status code.
How do I prevent this error?
Always check the Content-Type header and HTTP status code before parsing JSON. Only parse if status is 200-299 and Content-Type is application/json.