How to Fix 'Unexpected token < in JSON at position 0'
Why This Error Happens
This error occurs almost exclusively when your application calls JSON.parse() on a response that starts with '<' — almost always an HTML error page (like a 404 Not Found, 500 Internal Server Error, or cloudflare challenge page: <!DOCTYPE html>) instead of a valid JSON payload.
When an API route fails, crashes, or hits an unhandled exception, web servers often default to sending an HTML error page. When your JavaScript frontend executes response.json() or JSON.parse(text), the parser encounters the opening '<' from <!DOCTYPE html> or <html> at character index 0 and immediately throws this SyntaxError.
<!DOCTYPE html>
<html>
<head><title>500 Internal Server Error</title></head>
<body><h1>500 Internal Server Error</h1></body>
</html>{
"status": "error",
"statusCode": 500,
"message": "Internal Server Error"
}Step-by-Step Fix
- 1Check the HTTP response status before parsing: if (!response.ok) throw new Error(response.statusText).
- 2Inspect the network tab in browser DevTools to verify if the API returned an HTML page instead of Content-Type: application/json.
- 3Wrap JSON.parse in a try/catch block and inspect the raw text before parsing.
- 4Paste the response body into our online JSON Validator to check and sanitize the payload.
Test and Auto-Fix Broken JSON Online
Paste your raw payload into our free, client-side JSON Validator. Detect syntax errors with line numbers and fix them in one click.
Open Free JSON ValidatorFrequently Asked Questions
Why is the character at position 0 '<'?
The '<' character is the beginning of an HTML tag, such as '<!DOCTYPE html>' or '<html>'. When an API endpoint fails or returns a 404/500 error page, web servers return HTML instead of JSON.
How do I prevent this error in fetch()?
Always check response.ok or response.headers.get('content-type')?.includes('application/json') before calling response.json(). If not OK, read response.text() to debug the server error.
How can I auto-fix this with Format JSON Online?
Use our API Tester or JSON Validator tool. Paste your endpoint or payload directly to inspect headers, status codes, and valid JSON structure.