JSONRepair

Unexpected Token '<' in JSON at Position 0: Your API Returned HTML, Not JSON

JSON.parse says "unexpected token < at position 0" when the response is HTML, not JSON: an error page, a wrong URL, or an expired login. The three causes and the check-before-you-parse fix.

How do you fix the JSON.parse "unexpected token <" error at position 0? You stop parsing and start reading the response. That < is the first character of HTML, usually <!DOCTYPE html>, which means your code received a web page where it expected JSON. The bug is never in JSON.parse. It is in whatever handed the parser an HTML document.

The fix lands before the parse, not in it. Check the response status, read it as text, and look at the first 200 characters. Thirty seconds of that saves an hour of staring at your parser.

What the error message is actually telling you

Position 0 means the very first character. Valid JSON starts with {, [, a quote, a digit, or a literal like true. It never starts with <. So when V8 reports SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON, it is handing you the diagnosis: this string is HTML.

That is genuinely useful information. Most parse errors point at the spot where the parser gave up, several characters past the real mistake. This one points at character zero and names the intruder.

The three places the HTML comes from

In practice it is always one of three things. First, the server returned an error: a 500, a 404, a 401. Error pages are HTML, and if your fetch chain calls response.json() without checking response.ok first, the HTML error page goes straight into the parser.

Second, the URL is wrong. A typo, a path that changed, an API version bumped from v1 to v2. You asked for a web page and got one.

Third, your auth expired. The server answered with a login page because the token was missing or stale. This one is common in apps that worked yesterday: the token lifetime ran out and nobody noticed.

How to fix the "unexpected token <" error: check before you parse

My rule, and I apply it everywhere now: never call response.json() on a response you have not looked at.

const res = await fetch('/api/data');
if (!res.ok) {
  const body = await res.text();
  console.error('Non-OK response:', res.status, body.slice(0, 200));
  throw new Error(`HTTP ${res.status}`);
}
const data = await res.json();

Reading the raw text when the response is not OK tells you instantly whether you are looking at an HTML error page, a plain-text message, or something else entirely. You can also check the content type before parsing:

const contentType = res.headers.get('content-type');
if (!contentType || !contentType.includes('application/json')) {
  const text = await res.text();
  throw new Error('Expected JSON, got: ' + text.slice(0, 120));
}

One more habit worth building: log the raw text in the catch block, not just the error message. The error message says "unexpected token <". The raw text says "this is your company's branded 404 page." Only one of those tells you what to do.

A development-only variant worth knowing

If the error appears in development but not production (or the reverse), check your dev-server proxy. Frontend build tools rewrite API paths in dev; when the proxy rule is wrong or missing, the request falls through to the SPA fallback and returns index.html with a 200 status. Your code sees ok: true, parses, and dies on the <. The status check alone will not save you there, which is why the content-type check earns its keep.

When you hit this error, grab the raw response text and paste it into the JSON Repair Tool. It will flag the first character that breaks the grammar, which confirms whether the body is HTML, truncated JSON, or something weirder, so you stop guessing and start fixing the request.

Pasted JSON that looks fine but won't parse?

Drop it in. We strip BOMs, smart quotes, and control characters, and show you what changed.

Repair My JSON Free

Frequently asked questions

Why does JSON.parse say "unexpected token <" instead of "this is HTML"?

JSON.parse only sees text. It has no idea the text came from a server, and it does not know what HTML is. It reports the first character that violates the JSON grammar, which is the < at position 0.

Can I fix this with a JSON.parse option or a different parser?

No. There is no option that makes HTML parse as JSON, and no parser should try. The problem is upstream of the parser: the response itself is wrong. Fix the request, the URL, or the auth, not the parse call.

How do I see what the server actually sent?

Read the body as text before parsing: const text = await res.text(), then log text.slice(0, 200). If you need the response twice, use res.clone() so the original body stays readable.

Does this happen in Node.js too, or only browsers?

Both. Node's fetch (undici) behaves the same way: response.json() throws on an HTML body. The status and content-type checks work identically server-side.

Related reading: Unexpected Token 'u' in JSON at Position 0: You Parsed the Word "undefined" · Unexpected Token 'o' in JSON at Position 1: You Parsed an Object, Not a String · "Unexpected End of JSON Input": Empty Body, Cut-Off Payload, or Unclosed Bracket · Why Is My JSON Invalid? The 7 Syntax Errors That Break JSON.parse

One JSON gotcha a week, no fluff: Subscribe to the newsletter.