How-to guide

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.

Open the tool

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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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?
The response body began with a less-than sign, so it was HTML rather than JSON. The usual causes are an error page, a redirect to a login form, or a 404 page returned by a server that did not know the route was meant to be an API endpoint.
What does "Unexpected end of JSON input" mean?
The body ended before the document was complete. Either it was empty — a 204 response, or a request that returned nothing — or the connection dropped mid-transfer. Check the Content-Length header against what you actually received.
Why do I get "Unexpected token o in JSON at position 1"?
You called JSON.parse on something that was already an object. The object was converted to the string "[object Object]" first, and the parser stopped at the "o" in position 1. Remove the redundant parse.
Can a byte-order mark cause this?
Yes. A UTF-8 BOM at the start of a file is an invisible three-byte sequence that appears before the opening brace, and strict parsers reject it. Strip the BOM, which a text cleaner will do, or read the file as UTF-8 with BOM handling enabled.
Is the reported position always where the mistake is?
No. It is where parsing became impossible, which is often slightly after the real error. A missing comma is reported at the start of the next token rather than at the gap where the comma should have been.

Tools used in this guide

All tools →
All guides →

Last reviewed .