Unexpected token u in JSON at position 0: "undefined" is not valid JSON

JSON.parse converts its argument to a string first, and the JavaScript value undefined becomes the nine letters “undefined”. The parser rejects the first one, the u. So the JSON itself is rarely broken: the code passed in a value that does not exist yet. When “undefined” appears inside a document instead, it was written there by string concatenation.

Seen as:

  • SyntaxError: "undefined" is not valid JSON
  • SyntaxError: Unexpected token u in JSON at position 0
  • SyntaxError: Unexpected token 'u', ..."onToken": undefined,"... is not valid JSON
  • SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
  • SyntaxError: JSON Parse error: Unexpected identifier "undefined"

Input

Settings

History

Load from URL

Common causes

1. Parsing a variable that was never set

A missing environment variable, request field or object property is undefined, and JSON.parse(undefined) throws this exact error. Check for the value first or fall back to a default.

Before
const settings = JSON.parse(process.env.APP_SETTINGS);
After
const raw = process.env.APP_SETTINGS;
const settings = raw === undefined ? {} : JSON.parse(raw);

2. Storing undefined in localStorage

JSON.stringify(undefined) returns undefined, and localStorage.setItem stores it as the text “undefined”. Reading it back later fails. Note that a missing key returns null, which JSON.parse accepts.

Before
localStorage.setItem('cart', JSON.stringify(cart));
const saved = JSON.parse(localStorage.getItem('cart'));
After
if (cart !== undefined) localStorage.setItem('cart', JSON.stringify(cart));
const saved = JSON.parse(localStorage.getItem('cart') ?? 'null');

3. Building JSON with string concatenation

Concatenating values into a JSON string writes the word undefined whenever a value is missing. JSON.stringify builds the text correctly: it omits undefined properties and turns undefined array items into null.

Before
const body = '{"userId": ' + user.id + ', "sessionToken": ' + token + '}';
After
const body = JSON.stringify({ userId: user.id, sessionToken: token });

4. The word undefined inside a JSON document

JSON has no undefined value. Use null for “no value”, or leave the property out entirely. Fix it in PasteKit replaces undefined with null.

Before
{"userId": 7, "sessionToken": undefined}
After
{"userId": 7, "sessionToken": null}

Frequently asked questions

Why does JSON.parse(null) work but JSON.parse(undefined) fail?

Both are converted to strings first. null becomes “null”, which is valid JSON, while undefined becomes “undefined”, which is not. That is why a missing localStorage key (null) parses fine but a missing variable (undefined) throws.

Does JSON support undefined?

No. JSON values are objects, arrays, strings, numbers, true, false and null. JSON.stringify drops object properties whose value is undefined and writes null for undefined array items.

How do I find which value is undefined?

Log the argument with typeof right before the parse call. If it is the string “undefined”, look at where it was saved; if it is the value undefined, the variable or property was never set.

Related