Nifty Dev Tools
  1. Home
  2. Guides
  3. Unexpected token in JSON

Unexpected token in JSON

JSON.parse found a character that JSON doesn't allow at that spot: often NaN, a single quote, a comment or a trailing comma. Firefox reports it as “unexpected character” with a line and column.

The exact message

NaN, single quotes and a trailing comma (the worked example below)
{"id": 7, "score": NaN, "tags": ['math', 'code',]}
Chrome, Edge, Node.js
SyntaxError: Unexpected token 'N', ..." "score": NaN, "tags"... is not valid JSON
Firefox
SyntaxError: JSON.parse: unexpected character at line 1 column 20 of the JSON data
Safari
SyntaxError: JSON Parse error: Unexpected identifier "NaN"
A trailing comma in an array
[1, 2,]
Chrome, Edge, Node.js
SyntaxError: Unexpected token ']', "[1, 2,]" is not valid JSON
Firefox
SyntaxError: JSON.parse: unexpected character at line 1 column 7 of the JSON data
Safari
SyntaxError: JSON Parse error: Unexpected comma at the end of array expression
A string in single quotes
{"a": 'x'}
Chrome, Edge, Node.js
SyntaxError: Unexpected token ''', "{"a": 'x'}" is not valid JSON
Firefox
SyntaxError: JSON.parse: unexpected character at line 1 column 7 of the JSON data
Safari
SyntaxError: JSON Parse error: Single quotes (') are not allowed in JSON
A property without a value
{"a": }
Chrome, Edge, Node.js
SyntaxError: Unexpected token '}', "{"a": }" is not valid JSON
Firefox
SyntaxError: JSON.parse: unexpected character at line 1 column 7 of the JSON data
Safari
SyntaxError: JSON Parse error: Unexpected token '}'
A byte order mark (BOM, invisible) before the {
{"a": 1}
Chrome, Edge, Node.js
SyntaxError: Unexpected token '', "{"a": 1}" 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 ''
JSON.parse(undefined)
Chrome, Edge, Node.js
SyntaxError: "undefined" 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 "undefined"
JSON.parse called with an object, like JSON.parse({ a: 1 })
Chrome, Edge, Node.js
SyntaxError: "[object Object]" is not valid JSON
Firefox
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
Safari
SyntaxError: JSON Parse error: Unexpected identifier "object"

Measured in Chrome 158, Firefox 159, Safari's engine (WebKit 27.2) and Node.js 22. Edge uses the same engine as Chrome.

Chrome and Node.js name the character and quote the text around it: all of it when it's short, or a piece of it between ..." and "... when it's long. Firefox gives the line and column instead, and Safari often names the whole word, like “Unexpected identifier "NaN"”.

What it means

At that spot the parser found a character that can't start or continue anything in JSON. JSON is much stricter than JavaScript: no NaN, Infinity or undefined, no single quotes, no comments, no trailing commas and no missing values. The token tells you which of these it is:

TokenUsually meansFix
] or }a trailing comma before it, or no valueremove the comma, or add the value
'a string in single quotesuse double quotes
N, I, uNaN, Infinity, undefineduse null, a number or a string
T, FPython's True, Falseuse true, false
/a // or /* */ commentremove the comment
.a number like .5write 0.5
looks empty, ''a byte order mark (BOM) before the JSONsave the file as UTF-8 without BOM

A trailing comma before } gives a different message, “Expected double-quoted property name”: see Expected property name or '}' in JSON. If the token is < at the very start, you got an HTML page: see Unexpected token '<'.

When the message quotes your whole value

“"undefined" is not valid JSON” means JSON.parse got undefined, not text: the variable was never set, a function returned nothing, or a property doesn't exist. “"[object Object]" is not valid JSON” means it got an object, which it turned into the text [object Object]. That object is usually already parsed: await response.json() and libraries like axios (response.data) give you the data, not the JSON text. Use it as it is; don't parse it again.

What causes it

  • JSON written by hand or with string building, where it's easy to type JavaScript instead: single quotes, undefined, a comment, a trailing comma after the last item.
  • JavaScript or Python output that looks like JSON. console.log of an object, Python's print, or values like NaN that the program had in memory. Python's json.dumps writes NaN and Infinity unless you pass allow_nan=False.
  • Config files in JSONC or JSON5, which allow comments and trailing commas, read with a plain JSON parser.
  • A BOM added by an editor that saves "UTF-8 with BOM", when the file is read as text and passed to JSON.parse.

How to find the spot

Firefox's “line 1 column 20” is exact. Chrome and Node.js give no position for this message: search for the piece of text they quote. The quickest way is to paste the JSON into the JSON Formatter: it shows the line and column, underlines the character and Show me jumps to it.

How to fix it

For a one-off, press Fix it in the JSON Formatter: it repairs single quotes, comments, trailing commas, Python's True/False/None and replaces NaN, Infinity and undefined with null, listing every change.

To stop it from happening again, let a JSON library write the JSON instead of building it by hand. In JavaScript, JSON.stringify already writes null for NaN and Infinity:

JSON.stringify({ id: 7, score: NaN, tags: ["math", "code"] });
// {"id":7,"score":null,"tags":["math","code"]}

Worked example

JavaScript values that JSON doesn't have:

The input

{"id": 7, "score": NaN, "tags": ['math', 'code',]}
Chrome and Node.js say
SyntaxError: Unexpected token 'N', ..." "score": NaN, "tags"... is not valid JSON
The JSON Formatter says
Hmm, something's off on line 1, column 20: unexpected character “N” 🤔

Press “Fix all 4” and the JSON Formatter makes these changes:

  • Replaced NaN, Infinity or undefined with null (JSON doesn’t have them) (line 1)
  • Changed single quotes to double quotes in 2 strings (line 1)
  • Removed a trailing comma (line 1)

The result:

{"id": 7, "score": null, "tags": ["math", "code"]}

Open this example in the JSON Formatter

NaN becomes null, which changes the data: if the score matters, fix the code that produced NaN.