Skip to content
jsonbeautifiers
English
V8 Verified by running the parser on Node v24.15.0, V8 13.6.233.17.

"undefined" is not valid JSON

You passed undefined to JSON.parse. It gets converted to the string "undefined", and JSON has no such literal, so it fails at position 0. The real question is where the undefined came from, and there are only about three answers.

You may also see this written as

  • Unexpected token u in JSON at position 0

V8 rewrote most of its JSON error messages in 2022. Older wording is still what most search results show, but no current runtime emits it.

Paste your JSON and see exactly where it breaks

Input

Nothing you paste leaves your browser. The connect-src allowlist makes that a browser guarantee rather than a promise. Check it yourself

What actually causes it

Ranked by how often each one turns out to be the answer.

  1. 01 A storage key that was never set

    localStorage.getItem returns null for a missing key, and JSON.parse(null) actually works. But if the string "undefined" was ever written to that key, reading it back produces exactly this error, permanently, until the key is cleared.

    Breaks

    localStorage.setItem('token', someUndefinedValue);   // writes "undefined"
    JSON.parse(localStorage.getItem('token'));

    Works

    const raw = localStorage.getItem('token');
    const token = raw && raw !== 'undefined' ? JSON.parse(raw) : null;
  2. 02 An async value was not awaited

    Reading a property off a promise, or off state that has not loaded yet, gives undefined. The parse runs before the data arrives.

    Breaks

    const data = JSON.parse(fetchUser().body);

    Works

    const res = await fetchUser();
    const data = JSON.parse(await res.text());
  3. 03 A property that does not exist

    A renamed field, a typo, or an API that changed shape. obj.data when the payload uses obj.result gives undefined with no other warning.

Questions

Why does JSON.parse(null) work but JSON.parse(undefined) not?
Both are converted to strings first. null becomes "null", which is a valid JSON document, so it parses and returns null. undefined becomes "undefined", which JSON has no literal for.
How do I clear a key that holds the string "undefined"?
localStorage.removeItem(key), or guard the read as shown above. The bad value persists across reloads, which is why this error often looks intermittent between machines.

Fix it now

Paste the payload into the tool above, or go straight to the one built for this job.

Validate a payload