"undefined" is not valid JSON: JSON.parse was given undefined
By Santhi · Messages captured October 9, 2026 on 11 runtimes · How we test
JSON.parse turns its argument into a string before reading it. undefined becomes the nine letters "undefined", which is not JSON. So the message almost always means the variable you parsed was never set, not that a server sent the word undefined.
What each runtime prints
We ran these inputs through JSON.parse in each browser and Node version, and through json.loads in Python. These are the messages exactly as printed.
undefined
| Runtime | Exact message |
|---|---|
| Chrome 151Node 24Node 22Node 20 | "undefined" is not valid JSON |
| Firefox 153 | JSON.parse: unexpected character at line 1 column 1 of the JSON data |
| Safari 26 (WebKit)Bun | JSON Parse error: Unexpected identifier "undefined" |
| Node 18 | Unexpected token u in JSON at position 0 |
| Python 3.13Python 3.12Python 3.10 | JSONDecodeError: Expecting value: line 1 column 1 (char 0) |
{"a": undefined}| Runtime | Exact message |
|---|---|
| Chrome 151Node 24Node 22Node 20 | Unexpected token 'u', "{"a": undefined}" is not valid JSON |
| Firefox 153 | JSON.parse: unexpected character at line 1 column 7 of the JSON data |
| Safari 26 (WebKit)Bun | JSON Parse error: Unexpected identifier "undefined" |
| Node 18 | Unexpected token u in JSON at position 6 |
| Python 3.13Python 3.12Python 3.10 | JSONDecodeError: Expecting value: line 1 column 7 (char 6) |
- Chrome and Node 20+ put the word itself in quotes, "undefined" is not valid JSON, when the whole argument was undefined. When the word appears inside an object, they say "Unexpected token 'u'" and quote the whole text instead.
- Firefox says "unexpected character at line 1 column 1", the same message it gives for an HTML page or a byte order mark.
- Safari and Bun name the word: Unexpected identifier "undefined".
- Python has no undefined. It gets the text from a JavaScript client and says "Expecting value", at column 1 for the bare word and at the start of the value inside an object.
- JSON.parse(null) does not throw. String(null) is "null", which is valid JSON, so it returns null. That is why localStorage.getItem() for a missing key is safe and a missing property is not.
Edit it or paste your own. It runs in your browser; nothing is uploaded.
Common causes
- 1
A property or variable that does not exist
JSON.parse(data.payload) when the field is called body, a function that forgot to return, or an environment variable that is not set (process.env.CONFIG is undefined, not an empty string).
- 2
Reading storage as a property
localStorage.getItem('user') returns null for a missing key, and JSON.parse(null) is null. localStorage.user returns undefined for the same missing key, and JSON.parse throws.
- 3
Storing JSON.stringify(undefined)
JSON.stringify(undefined) returns undefined, not a string. localStorage.setItem() then stores the text "undefined", and every later read of that key fails with this error until it is removed.
- 4
JSON built with a template string
A body like `{"id": ${id}}` with an unset id becomes {"id": undefined}. JSON.stringify never writes undefined: it drops object keys whose value is undefined and writes null inside arrays.
How to fix it
Treat "no value" as its own case before parsing, and build JSON with JSON.stringify so undefined can never end up in the text.
JavaScript
function parseOrNull(text) {
if (text === undefined || text === null || text === '') return null;
return JSON.parse(text);
}
const user = parseOrNull(localStorage.getItem('user'));
// Clear a key that holds the text "undefined" from an earlier bug
if (localStorage.getItem('user') === 'undefined') localStorage.removeItem('user');Python
import json
raw = request.get_data(as_text=True)
# A JavaScript client sent String(undefined) or JSON.stringify(undefined)
data = None if raw.strip() in ("", "undefined") else json.loads(raw)- If you see quotes around undefined in the Chrome or Node message, look at the variable you passed, not the server. Log typeof value just before the parse.
- JSON Repair turns undefined inside an object into null and says so. It refuses the bare word undefined, because there is no data in it to repair.