← All articles

Unexpected Token u in JSON at Position 0: Parsing undefined

The "Unexpected token u" error means you called JSON.parse(undefined). Learn why undefined becomes "undefined", how to guard against it, and a safe-parse helper.

SyntaxError: Unexpected token 'u', "undefined" is not valid JSON (older engines: Unexpected token u in JSON at position 0) almost always means you called JSON.parse(undefined). JavaScript stringifies undefined to the text "undefined", and the parser chokes on the leading u. Here's why and how to fix it.

What the Error Looks Like

// V8 (Chrome / Node / Edge)
SyntaxError: "undefined" is not valid JSON
SyntaxError: Unexpected token 'u', "undefined" is not valid JSON
SyntaxError: Unexpected token u in JSON at position 0   // older V8

// Firefox
SyntaxError: JSON.parse: unexpected character at line 1 column 1

// Safari
SyntaxError: JSON Parse error: Unexpected identifier "undefined"

The u at position 0 is the first letter of the string "undefined" — the parser never received real data.

Why It Happens: You Parsed undefined

JSON.parse() coerces its argument to a string first. Pass undefined and it parses the literal text "undefined", which is not valid JSON. The value is usually undefined because:

  • A variable or function argument was never assigned
  • An object property doesn't exist (typo or wrong shape): obj.missing
  • An async value isn't ready yet (parsed before await)
  • A function with no return was used as the source

Broken example

const cached = localStorage.getItem('settings'); // missing key → null, OK
const draft  = window.appState?.draft;            // property missing → undefined
const data   = JSON.parse(draft);                 // ❌ "undefined" is not valid JSON

Fixed example

// Guard before parsing
const data = draft ? JSON.parse(draft) : null;

// Or a safe helper
function safeParse(text, fallback = null) {
  if (typeof text !== 'string' || text.trim() === '') return fallback;
  try { return JSON.parse(text); } catch { return fallback; }
}
const data2 = safeParse(draft);

Note: JSON.parse(null) does not throw — it returns null — and localStorage.getItem() returns null (not undefined) for a missing key. So a u error points specifically at an undefined value, not a missing storage key.

SyntaxError vs TypeError: Why You Get This One

A subtle but useful detail: JSON.parse(undefined) throws a SyntaxError, not a TypeError. Why? Because the spec says JSON.parse first coerces its argument to a string ("undefined") and then parses that string. The error is "this string isn't valid JSON," not "you passed the wrong type." That's why both JSON.parse(undefined) and JSON.parse("undefined") produce the identical position-0 message.

Contrast with APIs that do type-check, like JSON.stringify(BigInt(1))TypeError, or JSON.parse(Symbol())TypeError (Symbols can't be coerced to a string). If you catch parse errors, narrow on err instanceof SyntaxError to handle bad data separately from bad arguments.

How to Fix It — Step by Step

  1. Log the input:console.log(typeof src, src) right before the parse. If it prints undefined, that's the culprit.
  2. Trace where it should be set — an uninitialised state field, a missing argument, or a property that doesn't exist on the object.
  3. Add a guard (if (src)) or a safeParse fallback so an absent value doesn't reach JSON.parse().
  4. If it's async, make sure you await the value before parsing.

Frequently Asked Questions

What does "Unexpected token u in JSON at position 0" mean?

You passed undefined to JSON.parse(). It becomes the string "undefined", and the leading u is not a valid JSON start character.

Is it the same as the "token o" or "token <" error?

Same family, different value. u = undefined; token o = an object stringified to [object Object]; token < = an HTML response. The named character tells you what was actually passed.

Why doesn't JSON.parse(null) throw the same error?

"null" is valid JSON, so JSON.parse(null) returns null. Only undefined (→ "undefined") triggers the u error.

Fix It Now