How to Fix "Unexpected Token" JSON Errors
Unexpected token < in JSON at position 0 means the response was HTML rather than JSON, almost always an error page, a login redirect or a 404 returned with the wrong content type. Unexpected end of JSON input means the body was empty or truncated. In both cases the parser is right and the string being parsed is not what you assumed, so read the raw response first.
JSON Validator
Check whether JSON is valid and see exactly where it breaks.
Every one of these errors says the same thing in different words: the text handed to the parser was not JSON. The message names the first character that made parsing impossible, which is why the fix is almost never in your parsing code.
The single most useful habit is to log or inspect the raw response body before it reaches JSON.parse. A response that starts with `<!DOCTYPE` is a web page, and no amount of parser configuration will change that.
Step by step
-
Read the position number
Position 0 means the very first character was wrong, so the whole body is the wrong type. A position deep in the document means the payload is genuinely JSON but malformed somewhere specific — a different problem with a different fix.
-
Look at the raw body, not the parsed object
In the browser use the Network tab and read the Response tab rather than the Preview tab, which quietly renders HTML as though it were intentional. On the command line, print the body before parsing it.
-
Match the message to its cause
Unexpected token < means HTML. Unexpected end of JSON input means empty or truncated. Unexpected token o at position 1 means you passed an object that was already parsed. Unexpected token ' means single quotes, which JSON does not allow.
-
Check the status code and content type
A 500 page, a 302 to a login form and a rate-limit page are all HTML. Check the response status and the Content-Type header before assuming the endpoint is broken; an HTML body with a 200 status usually means a proxy or WAF answered instead of your API.
-
Validate the payload once you have it
If the body really is JSON and still fails, paste it into a validator to get the line and column. The reported position is where parsing became impossible, which is often just past the actual mistake.
Example
The body that produces "Unexpected token < in JSON at position 0". It is a web page, not an API response.
What arrived
<!DOCTYPE html>
<html><head><title>502 Bad Gateway</title></head>
<body><h1>502 Bad Gateway</h1></body></html>
What was expected
{
"error": "bad_gateway",
"message": "Upstream did not respond"
}
Frequently asked questions
What does "Unexpected token < in JSON at position 0" mean?
What does "Unexpected end of JSON input" mean?
Why do I get "Unexpected token o in JSON at position 1"?
Can a byte-order mark cause this?
Is the reported position always where the mistake is?
Tools used in this guide
All tools →- JSON Validator Check whether JSON is valid and see exactly where it breaks. In your browser
- JSON Formatter Beautify, validate and sort JSON with precise error messages. In your browser
- JSON Tree Viewer Explore a large JSON document as a collapsible tree. In your browser
- HTTP Status Code Reference Search every HTTP status code and what it means. In your browser
Related guides
All guides →- How to Format JSON Turn minified or broken JSON into something readable, and fix it when it will not parse.
- JSON Validator vs JSON Formatter Both parse your JSON. They differ in what they give you back and which one to reach for.
- How to Debug an API Request Work through a failing request in order, from the status code to the payload.
Last reviewed .