The exact message
<!DOCTYPE html>
<html lang="en">
<head><title>404 Not Found</title></head>
<body><h1>Not Found</h1></body>
</html>- Chrome, Edge, Node.js
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON- Firefox
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data- Safari
SyntaxError: JSON Parse error: Unrecognized token '<'
With fetch: await response.json()
- Chrome, Edge
SyntaxError: Failed to execute 'json' on 'Response': Unexpected token '<', "<!DOCTYPE "... is not valid JSON- Node.js
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON- Firefox
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data- Safari
SyntaxError: The string did not match the expected pattern.
<html><html><body>Bad Gateway</body></html>- Chrome, Edge, Node.js
SyntaxError: Unexpected token '<', "<html><bod"... is not valid JSON- Firefox
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data- Safari
SyntaxError: JSON Parse error: Unrecognized token '<'
Internal Server Error- Chrome, Edge, Node.js
SyntaxError: Unexpected token 'I', "Internal S"... is not valid JSON- Firefox
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data- Safari
SyntaxError: JSON Parse error: Unexpected identifier "Internal"
Measured in Chrome 158, Firefox 159, Safari's engine (WebKit 27.2) and Node.js 22. Edge uses the same engine as Chrome.
The < is the first character of an HTML page, and Chrome quotes the first few characters after it. A plain-text
error page gives the same message with a different letter, like “Unexpected token 'I'” for
Internal Server Error. With fetch, Safari doesn't mention the token at all: “The string did not match the expected
pattern.”
What it means
Your code expected JSON but received an HTML page. The parser stops at the very first character, so there is nothing wrong with any JSON: the question is why the server sent a web page instead.
What causes it
- A wrong URL. A typo, a missing
/apiprefix, or a relative URL:fetch("api/users")on the page/settings/requests/settings/api/users. The server answers with its 404 page, in HTML. - A single-page app's fallback. Hosts and dev servers set up for client-side routing answer every unknown path
with
index.htmland status200, so evenres.okis true. - The API isn't reachable in development. The frontend dev server answers the request itself because the proxy to the API isn't configured, or the API isn't running.
- An expired session. The API redirects to a sign-in page,
fetchfollows the redirect and gets its HTML. - An error page from the server, a proxy or a CDN. A
500,502or504usually comes as HTML or plain text, not as JSON.
How to find the spot
You don't need a line number here: the first character is the problem. Look at the whole response instead. In the browser's developer tools, select the request in the Network tab and check:
- the URL: is it the one you meant?
- the status:
404,401,500… (a200with HTML points to a single-page app's fallback) - the Content-Type response header:
text/htmlinstead ofapplication/json - the Response: the page's title often says what went wrong.
Pasting the response into the JSON Formatter confirms it: it stops at line 1, column 1, at the
<.
How to fix it
Fix the request, not the JSON: use the right URL (a path that starts with /, or the API's full address), set up the
dev server's proxy, start the API, or sign in again. Then make your code say what it got when it isn't JSON, instead
of failing with a parse error:
const res = await fetch(url);
const type = res.headers.get("content-type") ?? "";
if (!res.ok || !type.includes("json")) {
const text = await res.text();
throw new Error(`Expected JSON from ${url}, got ${res.status} ${type}: ${text.slice(0, 80)}`);
}
const data = await res.json();
For the 404 page below, this throws an error that starts with
Expected JSON from /api/user, got 404 text/html: <!DOCTYPE html>, which tells you exactly where to look.
Worked example
This is what a server typically sends for a URL it doesn't know:
The input
<!DOCTYPE html>
<html lang="en">
<head><title>404 Not Found</title></head>
<body><h1>Not Found</h1></body>
</html>- Chrome and Node.js say
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON- The JSON Formatter says
- Hmm, something's off on line 1, column 1: unexpected character “<” 🤔
Fix it isn't offered here: there's nothing to repair, because the text isn't broken JSON. It isn't JSON at all.