Unexpected token < in JSON at position 0
Your code asked for JSON and got a web page. The < at position 0 is the first character of <!DOCTYPE html> or <html>, and no JSON document can begin with it. The fault is almost never in the parser or in your parsing code; it is in the request that fetched the text.
Input
<!DOCTYPE html>
<html><body>502 Bad Gateway</body></html>Do
- Paste the input into the validator.
- Press Repair in the editor band.
Result
Nothing here could be repaired automatically.| Runtime | Message |
|---|---|
| Chrome 153 · Node.js 26 | Unexpected token '<', "<!DOCTYPE "... is not valid JSON |
| Node.js 18 (older V8) | Unexpected token < in JSON at position 0 |
| Firefox 156 | JSON.parse: unexpected character at line 1 column 1 of the JSON data |
| Safari 26 | JSON Parse error: Unrecognized token '<' |
| Python 3.14 json | Expecting value: line 1 column 1 (char 0) |
| This site | Unexpected "<"line 1, column 1 |
Recorded from each runtime; the wording is in Firefox’s, Safari’s and Python’s sources.
Two spellings of one message
Search results mix two wordings because V8, the engine inside Chrome, Edge and Node.js, rewrote its JSON messages between Node.js 18 and Node.js 20. Node.js 18 still prints the older form, Unexpected token < in JSON at position 0, and most answers written before then quote it. Current Chrome and Node.js print Unexpected token '<', "<!DOCTYPE "... is not valid JSON instead: the offending character in quotes, then up to the first ten characters of the text, cut off with three dots. That excerpt is the useful part. Ten characters are usually enough to recognise a doctype, a stray status line or the start of a sentence.
Firefox reports the same failure as an unexpected character at line 1 column 1, and Safari calls the angle bracket an unrecognized token. The Firefox wording has a page of its own, since it never shows the character at all.
Why a server answers JSON with HTML
- The address is wrong. A typo in the path, a missing
/apiprefix or a relative URL resolved against the wrong page returns the site's 404 page. Development servers for single-page apps are the classic case: they answer every unknown path withindex.html, so a misspelled endpoint comes back as the app's own HTML, with a status of 200. - The session expired. The request was redirected to a login form, and
fetchfollowed the redirect without telling you. - Something in between failed. A proxy, a load balancer or a CDN answered with its own 502 or 504 page because the service behind it was down or slow.
- The server crashed. A framework's debug page or a generic 500 page is HTML, even on a route that normally returns JSON.
Find out which one it is
Stop calling .json() blindly and look at what came back. Check the status and the Content-Type header first, and when either is wrong, read the body as text and put its beginning in the error:
const res = await fetch(url);
const type = res.headers.get('content-type') ?? '';
if (!res.ok || !type.includes('json')) {
const body = await res.text();
throw new Error(`${res.status} ${type}: ${body.slice(0, 200)}`);
}
const data = await res.json();The browser's developer tools answer the same question without code. Open the Network panel, repeat the request and select it: the status column and the Response tab show the page the server actually sent, and the page's title often names the problem outright, such as 502 Bad Gateway or Sign in.
The same message with other characters
The token in the message tells you what the text started with, and each one points at a different mistake.
'means single quotes, usually a Python dictionary or a JavaScript object literal; see Expected property name or '}'.- An invisible character at position 0 is a byte-order mark left at the start of a file.
"undefined" is not valid JSONmeansJSON.parsewas handedundefined, which it turns into the textundefinedbefore reading it. The variable you meant to parse was never filled in."[object Object]" is not valid JSONmeans the value was already an object. Something parsed it earlier, or it was never JSON text, so drop the secondJSON.parse.
NaN inside an otherwise valid object
An unexpected token can also appear midway through an object whose braces and quotes are correct. NaN means a calculation did not produce a number, but it is not one of JSON's allowed values. Chrome names the capital N; Firefox reports an unexpected character, and Safari identifies the whole word. This site's parser points to line 1, column 7 and quotes NaN in its message.
Input
{"n": NaN}Do
- Paste the input into the validator.
- Press Repair in the editor band.
Result
Replaced NaN and Infinity with null
{"n": null}| Runtime | Message |
|---|---|
| Chrome 153 · Node.js 26 | Unexpected token 'N', "{"n": NaN}" is not valid JSON |
| Node.js 18 (older V8) | Unexpected token N in JSON at position 6 |
| Firefox 156 | JSON.parse: unexpected character at line 1 column 7 of the JSON data |
| Safari 26 | JSON Parse error: Unexpected identifier "NaN" |
| Python 3.14 json | NO ERROR |
| This site | Unexpected "NaN"line 1, column 7 |
Recorded from each runtime; the wording is in Firefox’s, Safari’s and Python’s sources.
Python is the exception in this table: NO ERROR means the input was accepted. Python's json module reads and writesNaN unless told not to, even though this spelling falls outside JSON. Use allow_nan=False withjson.dumps to refuse it when writing; when reading, supply aparse_constant function that raises an error for these constants. Passing data through Python successfully therefore does not establish that a browser will accept it. Repair replaces the value withnull, which records absence rather than pretending the failed calculation produced zero. Decide whether your application should use a missing value, an explicit error field or a new calculation before adopting that replacement.
Python's None pasted as JSON text
None belongs in Python source and printed dictionaries, while JSON spells an absent value null. Even Python's own JSON decoder rejects the following text: accepting NaN is a separate extension, not permission to use Python expressions throughout a document. Use a JSON encoder to write a Python object, rather than replacing words inside its printed representation, since a string containing the word None may be legitimate data.
Input
{"n": None}Do
- Paste the input into the validator.
- Press Repair in the editor band.
Result
Converted Python literals
{"n": null}| Runtime | Message |
|---|---|
| Chrome 153 · Node.js 26 | Unexpected token 'N', "{"n": None}" is not valid JSON |
| Node.js 18 (older V8) | Unexpected token N in JSON at position 6 |
| Firefox 156 | JSON.parse: unexpected character at line 1 column 7 of the JSON data |
| Safari 26 | JSON Parse error: Unexpected identifier "None" |
| Python 3.14 json | Expecting value: line 1 column 7 (char 6) |
| This site | Unexpected "None"line 1, column 7 |
Recorded from each runtime; the wording is in Firefox’s, Safari’s and Python’s sources.
Repair recognises this Python literal and changes it tonull; its result keeps the original property name. Chrome's message begins with the same capital N as the NaN example, so read the quoted input as well as the token before deciding which mistake you have.
The word undefined used as a value
JavaScript permits undefined in memory, but JSON cannot store it as a literal. This input contains that word after a colon, which differs from passing an undefined variable directly to JSON.parse: the document has already started, and the error is at position 6 rather than at the beginning. A template that inserts an uninitialised variable into text can create exactly this object.
Input
{"n": undefined}Do
- Paste the input into the validator.
- Press Repair in the editor band.
Result
Replaced undefined with null
{"n": null}| Runtime | Message |
|---|---|
| Chrome 153 · Node.js 26 | Unexpected token 'u', "{"n": undefined}" is not valid JSON |
| Node.js 18 (older V8) | Unexpected token u in JSON at position 6 |
| Firefox 156 | JSON.parse: unexpected character at line 1 column 7 of the JSON data |
| Safari 26 | JSON Parse error: Unexpected identifier "undefined" |
| Python 3.14 json | Expecting value: line 1 column 7 (char 6) |
| This site | Unexpected "undefined"line 1, column 7 |
Recorded from each runtime; the wording is in Firefox’s, Safari’s and Python’s sources.
The repair output substitutes null for the unsupported word. Check the intended meaning first: JavaScript'sJSON.stringify omits an object property whose value is undefined, while an undefined array element becomes null. Those choices differ from retaining the key with a null value, which is what this example's repair does. Keeping serialisation in one encoder prevents these differences from being hidden inside hand-built JSON strings.
What this site does with it
Paste the text into the JSON Validator and the status bar reads Invalid JSON, with the first character underlined in Code view. The parser's own words are in the table's last row: it names the character it could not start a value with, at line 1, column 1.
Repair declines this one, and the refusal is deliberate. An HTML page often contains something that looks like JSON, a configuration blob inside a script tag for instance, and pulling that out would hand you a valid document that has nothing to do with the data you asked for. A wrong answer that parses is worse than an error, so the page is left alone and the question goes back to the request. For text that really is nearly JSON, the guide to fixing invalid JSON walks through what Repair can safely change.