TryBugHunt

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

SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON in JavaScript means you called res.json() or JSON.parse() on something that is not JSON — almost always an HTML error page from the server.

JavaScriptPattern: Type errors

How do I fix SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON?

SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON means res.json() or JSON.parse() got an HTML page instead of JSON. Usually the URL is wrong and returns a 404 page, or a login redirect sent HTML. Check if (!res.ok) before res.json(), and log await res.text() once to see what the server sent.

  1. 1. Log the raw bodyTemporarily replace res.json() with res.text() and log it. The HTML title usually explains the problem.
  2. 2. Check res.ok and statusfetch does not throw on 404 or 500. Check res.ok and res.status before parsing.
  3. 3. Check the URLCompare the request URL with the real API route. A typo often falls through to an HTML page.

What does SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON mean?

The first character JSON.parse saw was <, which starts HTML. Your request hit a 404 page, a login redirect, or a dev-server fallback, and the server sent HTML with a normal-looking response. Older browsers word it "Unexpected token < in JSON at position 0". "[object Object]" is not valid JSON (older: Unexpected token 'o') means you parsed something that was already an object; "Unexpected end of JSON input" means the body was empty.

Why does SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON happen?

1. The URL is wrong and returns an HTML 404

fetch does not throw on 404. The HTML error page goes straight into res.json().

Breaks

const res = await fetch("/api/user");
const data = await res.json();

Works

const res = await fetch("/api/users");
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();

2. Parsing a value that is already an object

res.json() and axios already parse. Parsing again stringifies to "[object Object]" first.

Breaks

const data = await res.json();
const user = JSON.parse(data);

Works

const user = await res.json();

3. An empty response body

A 204 No Content or an empty 200 has nothing to parse.

Breaks

const res = await fetch(url, { method: "DELETE" });
await res.json();

Works

const res = await fetch(url, { method: "DELETE" });
const data = res.status === 204 ? null : await res.json();

4. Authentication redirect returned HTML login page

An unauthenticated API request gets redirected by middleware to /login.

Breaks

const res = await fetch("/api/protected");
const data = await res.json();

Works

const res = await fetch("/api/protected");
const contentType = res.headers.get("content-type");
if (!contentType?.includes("application/json")) throw new Error("Expected JSON");
const data = await res.json();

5. Wrong API path in Next.js or Vite

Requesting a missing API path falls through to the single-page app HTML index.

Breaks

fetch("/api/typo-route");

Works

fetch("/api/correct-route");

How to find it in your own code

Replace res.json() with res.text() for a moment and log the result — you will see the HTML page or empty string immediately, and its title usually explains the problem. Then check res.ok before parsing, every time.

Practise type errors

None of the challenges raises this exact error yet. These practise the same kind of bug.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Related error guides

View all type errors →