Skip to content

How to fix “Unexpected token” and other JSON errors

What the most common JSON.parse and Python errors mean, the mistakes behind them, and how to fix each one in seconds.

Guide6 min readUpdated

A JSON parse error means the text isn't valid JSON, so the parser stops at the first character it can't make sense of. The message tells you what it expected and where. Once you know the eight mistakes behind almost every one of them, most errors take seconds to fix. The quickest route is to paste your JSON into the JSON Validator, which points to the exact line and column, or let JSON Repair fix the common cases automatically.

Reading the error message

The same mistake is worded differently depending on where the JSON is parsed. For a trailing comma:

Chrome / Node.js:  Expected double-quoted property name in JSON at position 8 (line 1 column 9)
Firefox:           JSON.parse: expected double-quoted property name at line 1 column 9 of the JSON data
Python:            json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 9 (char 8)

All three point to the same place. Position (or char) counts characters from 0; line and column count from 1. Go to that spot: the real mistake is usually right there or just before it.

“Unexpected token <”: you got HTML, not JSON

If the error mentions < at position 0, for example Unexpected token '<', "<!DOCTYPE "... is not valid JSON, there is nothing wrong with your parsing code. The server sent back an HTML page, typically a 404, a 500 error page or a login redirect.

const res = await fetch("/api/users");
if (!res.ok) throw new Error(`Request failed: ${res.status}`);
if (!res.headers.get("content-type")?.includes("application/json")) {
  throw new Error("Expected JSON, got " + (await res.text()).slice(0, 80));
}
const users = await res.json();
Check the status and Content-Type before parsing, so the real problem shows up in the error.

“Unexpected end of JSON input”

The parser reached the end of the text while it still expected more. Either the input is empty (an empty response body, an unset variable) or it was cut off partway, for instance a truncated file or a log line that hit a length limit. Log the raw text and its length before parsing to see which.

The 8 mistakes behind most JSON errors

1. A trailing comma

JavaScript allows a comma after the last item, JSON doesn't. After a comma the parser expects another key, finds } instead and stops. The same applies to arrays: [1, 2, 3,] is invalid.

{
  "name": "Ann",
  "age": 30,
}
Invalid
{
  "name": "Ann",
  "age": 30
}
Valid

Error in Chrome and Node.js: Expected double-quoted property name in JSON at position 32 (line 4 column 1)

2. Keys without double quotes

Every key in JSON is a string in double quotes. Object literals in JavaScript don't need them, which is why this one is so common when JSON is written by hand.

{ name: "Ann" }
Invalid
{ "name": "Ann" }
Valid

Error in Chrome and Node.js: Expected property name or '}' in JSON at position 2 (line 1 column 3)

3. Single quotes

JSON strings use double quotes only. Single-quoted text usually comes from Python's str() of a dictionary or from copying JavaScript; use json.dumps() or JSON.stringify() to produce real JSON instead.

{ "name": 'Ann' }
Invalid
{ "name": "Ann" }
Valid

Error in Chrome and Node.js: Unexpected token ''', "{ "name": 'Ann' }" is not valid JSON

4. Comments

JSON has no comment syntax. Config files that allow comments (such as VS Code's settings) use JSONC, a looser variant that standard parsers reject.

{
  "port": 8080 // default
}
Invalid
{
  "port": 8080
}
Valid

Error in Chrome and Node.js: Expected ',' or '}' after property value in JSON at position 17 (line 2 column 16)

5. A missing comma

Easy to miss when lines are added or reordered. The position in the error points just after the value that needs a comma.

{
  "a": 1
  "b": 2
}
Invalid
{
  "a": 1,
  "b": 2
}
Valid

Error in Chrome and Node.js: Expected ',' or '}' after property value in JSON at position 13 (line 3 column 3)

6. An unclosed bracket or brace

Every [ needs a ] and every { a }, closed in the right order. If the text simply stops (a cut-off download or log line), the error is Unexpected end of JSON input.

{ "tags": ["a", "b" }
Invalid
{ "tags": ["a", "b"] }
Valid

Error in Chrome and Node.js: Expected ',' or ']' after array element in JSON at position 20 (line 1 column 21)

7. Values JSON doesn't have

JSON values are strings, numbers, true, false, null, objects and arrays. NaN, Infinity, undefined, dates and functions aren't. Python's json.dumps writes NaN by default, so JSON from Python can fail in JavaScript; pass allow_nan=False to catch it early.

{ "total": NaN, "next": undefined }
Invalid
{ "total": null, "next": null }
Valid

Error in Chrome and Node.js: Unexpected token 'N', ..." "total": NaN, "next"... is not valid JSON

8. Raw line breaks or tabs inside a string

A string can't contain a real line break or tab; they must be escaped as \n and \t. This happens when JSON is built by joining strings by hand instead of with a JSON library.

{ "note": "line one
line two" }
Invalid
{ "note": "line one\nline two" }
Valid

Error in Chrome and Node.js: Bad control character in string literal in JSON at position 19 (line 1 column 20)

Less obvious causes

  • A byte order mark (BOM). Some editors on Windows save files with an invisible U+FEFF character at the start, which JSON.parse rejects. Save the file as “UTF-8” rather than “UTF-8 with BOM”, or strip it with text.replace(/^/, "").
  • Parsing twice. Calling JSON.parse on something that is already an object turns it into the string "[object Object]" first, which fails with Unexpected token 'o'. Libraries like axios already parse the response for you.
  • JSON stored as a string. If the text is full of backslashes, like "{\"id\":1}", it was stringified twice. Unescape it with JSON ⇄ String first.
  • Smart quotes. Text copied from a document or chat app may contain curly quotes “ ” instead of straight ones ".

Fixing it automatically

For hand-edited JSON, config snippets or data copied from JavaScript, JSON Repair fixes trailing commas, missing commas, single quotes, unquoted keys, comments, unclosed brackets and Python-style True/None in one step, and lists every change it made so you can check them. Then use the JSON Formatter to make the result readable. Everything runs in your browser, so the data you paste never leaves your device.

Frequently asked questions

Why does JSON.parse say “Unexpected token <”?

The text starts with <, which almost always means a server returned an HTML page (often an error or login page) instead of JSON. Check the request URL, the status code and the response's Content-Type before parsing.

Can JSON have comments or trailing commas?

No. Standard JSON (RFC 8259) allows neither. Formats such as JSON5 and JSONC accept them, but JSON.parse and most JSON libraries reject them.

How do I find which line has the error?

Paste the JSON into a validator. It reports the exact line and column of the first error and explains what was expected there.

Tools used in this guide

More guides