formatjson.pro
Validate, pretty-print or minify JSON in your browser — nothing is uploaded.

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.

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.