What does "Unexpected token < in JSON at position 0" mean?

Dark navy cover card with the title 'What does "Unexpected token < in JSON at position 0" mean?' in white and a green accent bar

"Unexpected token < in JSON at position 0" means your code tried to parse HTML as JSON. The very first character of the response was <, the start of <!DOCTYPE html> or a tag, and JSON can't begin with that. The server sent a web page, usually an error page, a login page or your own app's index.html, where you expected data.

Why "unexpected token in JSON at position 0" stops at the first character

The parser stops at position 0 because the very first character is already illegal. A JSON value starts with {, [, a quote, a digit, -, true, false or null. A < is none of those, so parsing fails before it reads anything else.

Newer Chrome and Node.js versions word the same error differently. In my test on Node.js v24.15.0, parsing an HTML page gave:

Unexpected token '<', "<!DOCTYPE "... is not valid JSON

Same error, new wording. Firefox has its own: JSON.parse: unexpected character plus a line and column, per the MDN list of JSON.parse errors.

Read the token: it tells you what came back

The character after "Unexpected token" is the first character of the body, so it's a clue to the cause. My Node.js tests gave these for common bodies:

Token in the error What the body probably was
< with "<!DOCTYPE "... A full HTML page: 404 page, login page, app shell
< with "<?xml vers"... An XML response, often a SOAP or storage error
< with " <br /> A PHP warning printed before your JSON
N with "Not Found" A plain-text error from the server or proxy
No token, "undefined" is not valid JSON You passed undefined, not a string
An invisible character A byte order mark (BOM) before the {

Read the quoted snippet in the newer message first. Its first few characters are often enough to recognise the page without opening DevTools.

The common causes of HTML instead of JSON

Five situations cause almost every case of HTML arriving where JSON should. This troubleshooting note names the first three: wrong URLs, login redirects and error pages.

  1. Wrong URL. A typo, a missing /api prefix or a relative path that resolves against the current page. The server answers with its 404 page in HTML.
  2. Expired session. The API redirects to a login page, fetch follows the redirect, and you get the login form's HTML.
  3. Server error page. The backend crashed and the framework or gateway rendered an HTML error page.
  4. Dev proxy that doesn't exist in production. Tools like Vite let you proxy requests whose path starts with /api to a backend. Create React App's docs say plainly that its proxy "only has effect in development". Deploy the same build and /api/users now goes to the static host.
  5. SPA fallback. A single-page app host returns index.html for any path it doesn't know.

Why response.ok doesn't catch the SPA fallback

response.ok doesn't catch this case because the fallback page often arrives with a success status. fetch() only rejects on network failures, not on HTTP errors, and Response.ok is true for any status from 200-299.

Vite's default appType is 'spa', which turns on an SPA fallback in the dev server. Cloudflare Pages does the same thing in production: with no top-level 404.html, it serves the root page for any path that doesn't match a file.

I checked this on onlineviewer.dev, a static site with no API. A curl of https://onlineviewer.dev/api/users came back 200 text/html. A fetch there passes the ok check, and then response.json() throws "Unexpected token '<'". Another static site I tried returned 404 text/html for a missing path. The status depends on the host.

How to fix it

Fix it by finding the page you actually received, then making the request reach the API. Quickest steps first.

  1. Open the Network tab. In DevTools, click the failing request and look at Response. It shows the HTML, the status and the real URL after any redirect.

  2. Log the raw text once. Swap res.json() for res.text() and print the first part:

    const text = await res.text();
    console.log(res.status, res.url, text.slice(0, 200));
    
  3. Fix the URL or the base path. Use an absolute API URL from config in production, or add the server-side route your dev proxy was faking.

  4. Check status and Content-Type before parsing. You get a clear message instead of a bare SyntaxError:

    const type = res.headers.get('content-type') || '';
    if (!res.ok || !type.includes('json')) {
      const body = await res.text();
      throw new Error(`Expected JSON from ${res.url}, got ${res.status} ${type}: ${body.slice(0, 200)}`);
    }
    const data = await res.json();
    
  5. Handle the login redirect. If res.url ends at your login page, treat it as signed out and send the user there.

With the raw text in hand, paste it into the OnlineViewer JSON validator to see whether it's JSON at all and where JSON.parse stops. It runs JSON.parse in the page and doesn't upload what you paste.

Other parse errors come from different problems, and their wording points the way. An empty body gives a different message, covered in what "Unexpected end of JSON input" means. If you're not sure which of JSON.parse and JSON.stringify you need, see JSON.stringify vs JSON.parse. For real JSON with small mistakes, like a trailing comma, read how to fix invalid JSON automatically.

My take

Put the Content-Type check from step 4 in your shared fetch helper and stop calling res.json() straight on a response. It costs a few lines. The error it throws names the URL, the status and the start of the body, everything you'd otherwise dig out of DevTools.

The limitation: a Content-Type check trusts the server, and servers lie. Some APIs send JSON as text/plain, and some send an HTML error page labelled application/json. Keep a try/catch around the parse and log the raw text when it fails.

FAQ

Why do I get Unexpected token < in JSON at position 0?

Your code called JSON.parse or response.json() on HTML. The first character of an HTML page is <, so the parser fails on the very first character. Log the raw text to see which page came back.

Why does it work locally but fail in production?

A development proxy often forwards /api requests to your backend, and it only runs in development. In production the same relative URL hits the static host, which returns an HTML page instead.

Is checking response.ok enough to prevent it?

No. A single-page app fallback can return your index.html with a 200 status, so response.ok is true. Check the Content-Type header too.

What does Unexpected token N in JSON mean?

The body started with the letter N, which usually means a plain-text reply such as Not Found. The first character after Unexpected token tells you what the server actually sent.

Sources