SyntaxError: Unexpected token u in JSON at position 0
You passed undefined to JSON.parse(). JavaScript coerces undefined to the string "undefined", and the parser fails on its first character — the letter 'u' at position 0. The real problem is upstream: the value you tried to parse was never set. Fix the source, don't parse undefined.
Runs entirely in your browser — your data stays local. It flags the undefined-passed-to-parse cause behind token u so you fix the source value, not chase the parser.
How to fix "Unexpected token u in JSON at position 0"
- Trace the undefined value. The u comes from the word undefined: you passed undefined into JSON.parse(). This usually happens when a fetch, localStorage.getItem(), or API field returned nothing. Log the value right before parsing.
- Guard before parsing. Add a check: const raw = localStorage.getItem(key); const data = raw ? JSON.parse(raw) : null; So a missing key yields null instead of crashing the parser.
- Fix the missing source. If the value should exist, fix the write side — confirm the key name, that the fetch resolved, and that the field is actually present. Paste the real payload into the tool above to validate it.
What the error means
JSON.parse() needs a string. When you hand it undefined, JavaScript converts it to the text "undefined". The parser reads position 0 and sees u, which is not a valid JSON start, so it throws.
The usual cause
The value was never there. Classic sources: localStorage.getItem('key') for a key that doesn't exist (returns null → or an undefined variable), a function that returned nothing, an object property that doesn't exist, or an API field that was missing. You then called JSON.parse() on that empty value.
Fix it
1. Guard before parsing: const raw = localStorage.getItem('key'); const data = raw ? JSON.parse(raw) : null;
2. Trace the source. console.log(typeof value, value) right before the parse — if it logs undefined, fix where it should have been assigned.
3. Don't double-handle. If the value already holds an object, don't parse it at all.
FAQ
Why 'u' and position 0?
undefined stringifies to "undefined", whose first character is 'u' at position 0 — an invalid JSON start.
I see 'Unexpected token o' instead — related?
Yes, same family. 'o' comes from "[object Object]": you parsed a real object. 'u' comes from "undefined": you parsed a missing value.
Best way to prevent it?
Never parse a value you haven't checked. Use a ternary or default (raw ?? '{}') so JSON.parse always receives a real JSON string.