ENRU

"undefined" is not valid JSON: the wrong value reached JSON.parse

Updated

SyntaxError: "undefined" is not valid JSON has nothing to do with your JSON. JSON.parse was given undefined, turned it into the nine letters undefined and refused them. Its twin, "[object Object]" is not valid JSON, means it was given an object, usually one that had already been parsed. There was no JSON, only a variable that should have held some. The fix is wherever that value came from.

Why JSON.parse says this

JSON.parse converts its argument to a string before parsing. undefined becomes "undefined", any plain object becomes "[object Object]", a promise "[object Promise]". None of these is JSON: RFC 8259 allows exactly three bare words, true, false and null, and undefined is not among them. null is the trap: it becomes "null", which is valid, so JSON.parse(null) quietly returns null.

ArgumentV8 message (Node.js 25.1, Chrome 156)What happened
undefinedSyntaxError: "undefined" is not valid JSONThe value was never set
the string "undefined"SyntaxError: "undefined" is not valid JSONundefined was saved as text earlier
{ id: 42 }SyntaxError: "[object Object]" is not valid JSONAlready parsed, or stored without JSON.stringify
[{ id: 42 }]SyntaxError: "[object Object]" is not valid JSONAn array of objects converts to the same text
a PromiseSyntaxError: Unexpected token 'o', "[object Promise]" is not valid JSONA missing await
a ResponseSyntaxError: Unexpected token 'o', "[object Response]" is not valid JSONres passed instead of await res.text()
nullno error, returns nulllocalStorage.getItem of a missing key

V8 gives undefined, NaN, Infinity and [object Object] this short form without “Unexpected token”; other text gets the longer form that names the first bad character.

The same mistake in other engines

Engine, version checkedJSON.parse(undefined)JSON.parse({})
Chrome 156, Node.js 20.20 and 25.1 (V8)"undefined" is not valid JSON"[object Object]" is not valid JSON
Node.js 18.20 (V8 10.2)Unexpected token u in JSON at position 0Unexpected token o in JSON at position 1
Firefox (source: SpiderMonkey)JSON.parse: unexpected character at line 1 column 1 of the JSON dataJSON.parse: unexpected character at line 1 column 2 of the JSON data
Safari, JavaScriptCore of macOS 27.0.1JSON Parse error: Unexpected identifier "undefined"JSON Parse error: Unexpected identifier "object"
React Native, Hermes (source)JSON Parse error: Unexpected character: uJSON Parse error: Unexpected character: o
Python 3.9.6 and 3.14.10, json.loads(None) and json.loads({})TypeError: the JSON object must be str, bytes or bytearray, not NoneTypeTypeError: the JSON object must be str, bytes or bytearray, not dict

The older V8 wording explains a classic puzzle: in Unexpected token o in JSON at position 1 the o is the second character of [object Object], and in Unexpected token u in JSON at position 0 the u starts undefined. Python refuses the wrong type before parsing, so its message is a TypeError, not a JSONDecodeError.

Where the wrong value comes from

undefined

  • Storage read as a property. localStorage.user is undefined for a missing key, while localStorage.getItem('user') is null.
  • undefined saved as text. localStorage.setItem('user', undefined) and setItem('user', JSON.stringify(undefined)) both store the text undefined: JSON.stringify(undefined) returns undefined, and storage turns it into a string. The error then appears on the next page load, far from the bug.
  • A missing field or attribute. JSON.parse(el.dataset.config) without a data-config attribute, or JSON.parse(body.payload) when the sender left the field out.
  • An unset environment variable, as in JSON.parse(process.env[name]).
  • A function without return whose result goes straight into JSON.parse.

[object Object]

  • Double parse. await res.json(), axios’s response.data, jQuery with dataType: 'json' and Express with express.json() already hand you objects. Parsing them again produces this error.
  • Stored without JSON.stringify. localStorage.setItem('user', user) stores the text [object Object].
  • String building. A template literal, String(user) and '' + user all produce [object Object].
  • postMessage. event.data arrives as a structured clone, already an object, unless the sender called JSON.stringify.

[object Promise] and [object Response]

A missing await: JSON.parse(res.json()) passes a promise, and JSON.parse(res) passes the response object itself. Use await res.json() and stop there.

Fix: parse strings only, stringify everything you store

Find out what actually reached the parser, fix the code that wrote it, and make the reader tolerate what is already stored. Checked in Chrome 156:

console.log(typeof value, value);

localStorage.setItem('user', JSON.stringify(user));

function readStored(key, fallback = null) {
  const raw = localStorage.getItem(key);
  if (raw === null || raw === 'undefined') return fallback;
  try {
    return JSON.parse(raw);
  } catch {
    return fallback;
  }
}

const data = typeof body === 'string' ? JSON.parse(body) : body;

With TypeScript, honest types catch most of this at compile time. Under strict, passing a string | undefined to JSON.parse fails with TS2345: “Argument of type 'string | undefined' is not assignable to parameter of type 'string'.” (TypeScript 5.9.3). A cast with as string or any switches the check off and sends the error back to runtime.

Prevent it

  • Keep one pair of storage helpers: one that always stringifies on write, one that checks on read.
  • Let the HTTP client parse. res.json(), axios and express.json() return objects; never parse their result again.
  • Type parser input as string, never any.
  • Clean up once: remove stored keys whose value is the text undefined or [object Object], so old visitors stop hitting the error.

Try it

Paste the text you actually have into the validator. The bare word undefined is reported as an invalid literal, because JSON knows only true, false and null; see invalid literal.

Related: Unexpected token '<' when a web page arrives instead of JSON, Unexpected end of JSON input for empty text, and the list of JSON errors.

"undefined" is not valid JSON FAQ

Why does JSON.parse(undefined) throw but JSON.parse(null) doesn’t?

Both are converted to strings first. null becomes "null", which is valid JSON; undefined becomes "undefined", which isn’t.

What does “"[object Object]" is not valid JSON” mean?

JSON.parse got an object, not a string. Usually it is data that was already parsed being parsed a second time, or an object stored without JSON.stringify.

Is the JSON from my server broken?

No. This message is about the argument, not about any response. Log typeof value right before the call to see what arrived.

Why does it happen only after a reload?

Something stored the text undefined or [object Object] earlier, and the reload reads it back. Fix the write and remove the bad key.