"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.
| Argument | V8 message (Node.js 25.1, Chrome 156) | What happened |
|---|---|---|
undefined | SyntaxError: "undefined" is not valid JSON | The value was never set |
the string "undefined" | SyntaxError: "undefined" is not valid JSON | undefined was saved as text earlier |
{ id: 42 } | SyntaxError: "[object Object]" is not valid JSON | Already parsed, or stored without JSON.stringify |
[{ id: 42 }] | SyntaxError: "[object Object]" is not valid JSON | An array of objects converts to the same text |
a Promise | SyntaxError: Unexpected token 'o', "[object Promise]" is not valid JSON | A missing await |
a Response | SyntaxError: Unexpected token 'o', "[object Response]" is not valid JSON | res passed instead of await res.text() |
null | no error, returns null | localStorage.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 checked | JSON.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 0 | Unexpected token o in JSON at position 1 |
| Firefox (source: SpiderMonkey) | JSON.parse: unexpected character at line 1 column 1 of the JSON data | JSON.parse: unexpected character at line 1 column 2 of the JSON data |
| Safari, JavaScriptCore of macOS 27.0.1 | JSON Parse error: Unexpected identifier "undefined" | JSON Parse error: Unexpected identifier "object" |
| React Native, Hermes (source) | JSON Parse error: Unexpected character: u | JSON 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 NoneType | TypeError: 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.userisundefinedfor a missing key, whilelocalStorage.getItem('user')isnull. undefinedsaved as text.localStorage.setItem('user', undefined)andsetItem('user', JSON.stringify(undefined))both store the textundefined:JSON.stringify(undefined)returnsundefined, 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 adata-configattribute, orJSON.parse(body.payload)when the sender left the field out. - An unset environment variable, as in
JSON.parse(process.env[name]). - A function without
returnwhose result goes straight intoJSON.parse.
[object Object]
- Double parse.
await res.json(), axios’sresponse.data, jQuery withdataType: 'json'and Express withexpress.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'' + userall produce[object Object]. postMessage.event.dataarrives as a structured clone, already an object, unless the sender calledJSON.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 andexpress.json()return objects; never parse their result again. - Type parser input as
string, neverany. - Clean up once: remove stored keys whose value is the text
undefinedor[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.