FormatterHQ

Debugging

Fix 'Unexpected token < in JSON at position 0'

The 'Unexpected token < in JSON' error almost always means your code got HTML instead of JSON. How to confirm the real cause in seconds, and fix each variant.

Published 5 min read

“Unexpected token < in JSON at position 0” is one of the most-searched JavaScript errors, and the good news is that it almost always means one specific thing: your code tried to parse HTML as if it were JSON. The < is the start of <!DOCTYPE html>. This guide shows how to confirm that in seconds, the handful of underlying causes, and how to fix each. It is the runtime-error companion to our more general guide on debugging invalid JSON.

What the error is really telling you

A JSON parser reads from the first character. “Position 0” means it failed immediately, and the token < means the first character was an angle bracket — the opening of an HTML tag. So the data you handed JSON.parse() (or that response.json() tried to parse) was an HTML document, not JSON. The parser is working correctly; the input is wrong.

Confirm it in one step

Before changing anything, look at what actually arrived. Read the response as text and log it instead of parsing:

const res = await fetch(url);
const text = await res.text();      // read raw, do not parse yet
console.log(res.status, text.slice(0, 200));

If the log shows <!DOCTYPE html> or <html>, you have your answer: the server returned a web page. Now the question is why.

The common causes (and fixes)

  • Wrong URL → 404 page. The endpoint is mistyped or a relative path resolved against the wrong base, so the server returned its HTML 404 page. Fix the URL; check it is absolute or resolves correctly.
  • Not authenticated → login/redirect page. The API redirected to an HTML sign-in page because the request lacked a valid token or cookie. Fix the auth header; confirm the session.
  • Server error → HTML error page. A 500 produced a framework error page in HTML. Check res.status — and fix the server, or at least handle the non-200 case.
  • Proxy or firewall page. A corporate proxy or WAF returned an HTML challenge instead of passing the request through.
  • Hitting the app, not the API. In a single-page app, a missing API route can fall through to index.html, so you get the app shell back as HTML.

The real fix: check before you parse

The robust pattern is to never parse blindly. Verify the response is OK and actually JSON first:

const res = await fetch(url);
if (!res.ok) {
  throw new Error(`Request failed: ${res.status}`);
}
const type = res.headers.get("content-type") || "";
if (!type.includes("application/json")) {
  const body = await res.text();
  throw new Error(`Expected JSON, got ${type}: ${body.slice(0, 120)}`);
}
const data = await res.json();   // safe now

This turns a cryptic parse error into a clear message that tells you the status and the content type, so the real problem (a 404, a 500, a redirect) is obvious.

Close relatives of this error

  • “Unexpected token o in JSON at position 1” — you called JSON.parse() on a value that is already an object. The o comes from the string [object Object]. Remove the extra parse; response.json() is already parsed.
  • “Unexpected end of JSON input” — the body was empty or truncated (a 204 No Content, or a connection cut short). Guard against an empty body before parsing.
  • “Unexpected token , in JSON” — this one is malformed data: usually a trailing comma. Run the text through the JSON fixer to repair it, or the validator to find the exact spot.

When the data really is broken JSON

If the log shows JSON-ish text rather than HTML — a stray comma, a missing bracket, a smart quote pasted from a document — then you have genuinely invalid JSON, not the wrong content type. Paste it into the validator to get the exact line and column, format it to see the structure, or let the JSON fixer repair common mistakes automatically. Our guide on debugging invalid JSON walks through those data-level errors in depth, and working with JSON in JavaScript covers safe parsing patterns.

Prevent it on the server

If you own the API, the cleanest fix is upstream: make it always return JSON, even for errors. A frontend gets this error because a 404, a 500 or an auth redirect produced an HTML page. Configure your framework's error handlers so that requests under your API path return a JSON error body ({"error": "Not found"}) with the right status code and an application/json content type, rather than falling through to an HTML error page or the SPA's index.html. When the server is consistent, the client never has to guess what it received, and a failed request surfaces as a clean JSON error instead of a parse crash.

Prevent it in your client code

On the client, centralise fetching so the check happens in one place instead of at every call site. A small wrapper that verifies res.ok and the content type, reads the body once, and throws a descriptive error means a bad response is reported the same clear way everywhere — and you are never one forgotten check away from the cryptic "Unexpected token" again. Pair that with logging the first part of any unparseable body, so when something does go wrong in production you can see immediately whether you got HTML, an empty body, or genuinely malformed JSON, and reach for the validator or fixer accordingly.

What HTTP clients do for you

Some libraries soften this error. Axios parses JSON automatically and exposes the parsed body on response.data, and it rejects non-2xx responses by default — so a 404 becomes a caught error, not a parse crash. The native Fetch API does neither: it does not throw on 404 and does not parse for you, which is exactly why the manual res.ok and content-type checks shown above matter. Whichever client you use, the principle holds: decide what a failure looks like before you touch the body, so a wrong response never reaches JSON.parse() unguarded.

Key takeaways

  • “Unexpected token < at position 0” means HTML was parsed as JSON — usually an error, login or 404 page.
  • Log res.status and the raw res.text() to see what really came back.
  • Fix the request (URL, auth, method); check res.ok and the content type before calling .json().
  • “token o at position 1” means you double-parsed an object; “unexpected end” means an empty or truncated body.
  • If the body really is malformed JSON, use the validator and fixer.
  • JSON
  • JavaScript
  • Debugging
  • Fetch

Frequently asked questions

What does &#039;Unexpected token &lt; in JSON at position 0&#039; mean?
Your code tried to parse something that starts with < — almost always an HTML page (<!DOCTYPE html>) returned by an API instead of JSON. The parser fails on the very first character, position 0.
Why is my API returning HTML instead of JSON?
Common causes: the URL is wrong and hit a 404 page, the request failed and returned an error page, a login/redirect page was served, or a proxy returned an HTML error. Log the raw response text to see what actually came back.
How do I fix &#039;Unexpected token &lt; in JSON&#039;?
Stop parsing blindly. Check response.ok and the Content-Type header before calling .json(), and log the raw text when parsing fails. Fix the underlying request (URL, auth, method) so the server actually returns JSON.
What about &#039;Unexpected token o in JSON at position 1&#039;?
That means you called JSON.parse() on something that is already a JavaScript object (the o is from [object Object]). Do not parse the result of response.json() again — it is already parsed.
Is this error different from invalid JSON data?
Usually yes — this error is about receiving the wrong kind of content (HTML, not JSON). For genuinely malformed JSON data, see how to debug invalid JSON and run it through the validator.

Tools used in this guide