json

FormatValidateConvert

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

  1. Paste the input into the validator.
  2. Press Repair in the editor band.

Result

Nothing here could be repaired automatically.

Try it in the validator →

What each runtime prints for this input
RuntimeMessage
Chrome 153 · Node.js 26Unexpected token '<', "<!DOCTYPE "... is not valid JSON
Node.js 18 (older V8)Unexpected token < in JSON at position 0
Firefox 156JSON.parse: unexpected character at line 1 column 1 of the JSON data
Safari 26JSON Parse error: Unrecognized token '<'
Python 3.14 jsonExpecting value: line 1 column 1 (char 0)
This siteUnexpected "<"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 /api prefix 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 with index.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 fetch followed 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 JSON means JSON.parse was handed undefined, which it turns into the text undefined before reading it. The variable you meant to parse was never filled in.
  • "[object Object]" is not valid JSON means the value was already an object. Something parsed it earlier, or it was never JSON text, so drop the second JSON.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

  1. Paste the input into the validator.
  2. Press Repair in the editor band.

Result

Replaced NaN and Infinity with null

{"n": null}

Try it in the validator →

What each runtime prints for this input
RuntimeMessage
Chrome 153 · Node.js 26Unexpected token 'N', "{"n": NaN}" is not valid JSON
Node.js 18 (older V8)Unexpected token N in JSON at position 6
Firefox 156JSON.parse: unexpected character at line 1 column 7 of the JSON data
Safari 26JSON Parse error: Unexpected identifier "NaN"
Python 3.14 jsonNO ERROR
This siteUnexpected "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

  1. Paste the input into the validator.
  2. Press Repair in the editor band.

Result

Converted Python literals

{"n": null}

Try it in the validator →

What each runtime prints for this input
RuntimeMessage
Chrome 153 · Node.js 26Unexpected token 'N', "{"n": None}" is not valid JSON
Node.js 18 (older V8)Unexpected token N in JSON at position 6
Firefox 156JSON.parse: unexpected character at line 1 column 7 of the JSON data
Safari 26JSON Parse error: Unexpected identifier "None"
Python 3.14 jsonExpecting value: line 1 column 7 (char 6)
This siteUnexpected "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

  1. Paste the input into the validator.
  2. Press Repair in the editor band.

Result

Replaced undefined with null

{"n": null}

Try it in the validator →

What each runtime prints for this input
RuntimeMessage
Chrome 153 · Node.js 26Unexpected token 'u', "{"n": undefined}" is not valid JSON
Node.js 18 (older V8)Unexpected token u in JSON at position 6
Firefox 156JSON.parse: unexpected character at line 1 column 7 of the JSON data
Safari 26JSON Parse error: Unexpected identifier "undefined"
Python 3.14 jsonExpecting value: line 1 column 7 (char 6)
This siteUnexpected "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.