Unexpected token in JSON
Why 'Unexpected token in JSON at position 0' happens and how to fix it. Usually the text is not JSON at all: an HTML error page, an empty response, or a stray character.
Open the JSON formatterWhat the error looks like
Different engines word this differently. You may see any of these:
Why it happens
- Position 0 means the very first character was already wrong, so in most cases the text is not JSON at all.
- The classic case is a server returning an HTML error page instead of JSON. The first character is < from <!DOCTYPE html> or <html>, which is why the message so often names the < token.
- 'Unexpected token o in JSON at position 1' usually means an object was passed where a string was expected: something called JSON.parse on a value that had already been converted to the string "[object Object]".
- An empty response also lands here, because an empty string is not a valid JSON document.
Example
<!DOCTYPE html>
<html>
<body>404 Not Found</body>
</html>
{
"error": "Not Found",
"status": 404
}
How this tool reports it
The formatter names the character it found and the line and column it found it on, rather than a byte offset, so you can see immediately whether you are looking at JSON or at something else entirely.
Paste your JSON into the formatter to see the exact line and column, or use the validator if you only need to check it.
Worth knowing
- Before debugging the JSON, log the raw response body. If it starts with < you have an HTML page, and the real problem is the request, not the parsing.
- Check the response status and content type as well. A 500 with an HTML body produces exactly this error.