JSON.parse coerces its argument to a string before it starts. String(undefined) is the nine-letter word undefined. No valid JSON document starts with the letter u, so the parser dies on the very first character. That is the whole mechanism. Everything else is a story about how an undefined got into that variable without you noticing.
Where the undefined comes from
The three classics:
// 1. A localStorage key that does not exist yet
const raw = localStorage.getItem("settings"); // null on first visit
JSON.parse(raw); // "Unexpected token n..." in modern engines (String(null) is "null")
// 2. Parsing an undefined variable outright
JSON.parse(config.settings); // "Unexpected token u..." when config.settings is undefined
// 3. Parsing before the async fetch finished
let payload;
fetch("/api/user").then(r => r.text()).then(t => payload = t);
const user = JSON.parse(payload); // runs first; payload is still undefined
Note the localStorage trap: getItem returns null, which stringifies to "null" and throws a token-n error instead. Same family, same fix. The async one is the meanest of the three because it fails only when the network wins the race, which means it passes every test on your fast office connection and blows up in production.
A fourth variant shows up inside the JSON text itself. People copy a JavaScript object literal into a JSON file and leave a literal undefined as a value: {"retries": undefined}. JSON has no undefined keyword. JSON.stringify silently drops keys with undefined values, which is why the object looked fine in the console and broke in the file. Delete the key, or use null if the slot has to exist.
How to fix JSON.parse unexpected token u at position 0
Fix the input, not the parse call. Before you parse, prove the value is a non-empty string:
const raw = localStorage.getItem("settings");
if (typeof raw === "string" && raw.length > 0) {
const settings = JSON.parse(raw);
}
The typeof check matters. A bare truthiness check passes for objects too, and JSON.parse on an already-parsed object gives you the token-o error instead (from the string "[object Object]"). Same guard family: do not parse what is not a string.
For fetch, await the body before parsing, or let the response do it:
// prefer this: response.json() parses the actual body
const user = await (await fetch("/api/user")).json();
// if you must read text first, check emptiness before parsing
const res = await fetch("/api/user");
const text = await res.text();
if (!text) throw new Error("Empty response body from /api/user");
const user = JSON.parse(text);
The safeParse habit
Every codebase I have touched ends up parsing JSON from sources it does not control: caches, queues, databases, webhooks. I stopped debugging this error one occurrence at a time and wrote one helper I drop into everything:
function safeParse(value, fallback = null) {
if (typeof value !== "string" || value.trim() === "") return fallback;
try {
return JSON.parse(value);
} catch (err) {
console.warn("parse failed; first 120 chars:", value.slice(0, 120));
return fallback;
}
}
The console.warn is the important line. The old behavior, crashing on garbage, at least told you something was wrong. A helper that silently returns null hides the same corruption you were trying to see. Logging the first chunk of the offending input keeps the failure visible while the rest of the page stays up. One bad row in a list endpoint no longer 500s the whole list.
This error is one our repair tool cannot fix, and I would rather say that plainly than let you find out by trying. There is no broken JSON to repair. The parser never saw JSON at all. Paste the actual payload into the repair tool once you are sure the variable holds a string; if it holds undefined, no tool can conjure the missing data. The fix lives one step earlier in your code, and now you know where to look.
Holding a string that still won't parse?
Paste it in and get valid JSON back, with each fix explained.
Repair My JSON FreeFrequently asked questions
What does "Unexpected token u in JSON at position 0" mean?
You passed undefined (or a value JavaScript coerced to the string "undefined") into JSON.parse. The parser read the first character, "u", and rejected it. The bug is in the value you handed the parser, not in your JSON.
Why does JSON.parse(undefined) throw that error?
JSON.parse coerces its argument to a string first. String(undefined) is the word "undefined", which starts with "u". Since no valid JSON starts with "u", the parser fails at position 0.
How do I fix "Unexpected token u in JSON at position 0"?
Find where the value became undefined before it reached JSON.parse: a missing localStorage key, an async fetch that had not resolved, or a database field that came back null. Guard with a typeof check or wrap parsing in a safeParse helper that returns a fallback instead of throwing.
Is the problem my JSON or my JavaScript?
Your JavaScript. The parser never saw real JSON. The error tells you the input to JSON.parse was not the string you assumed, which means the bug is one step earlier in your code: the value was undefined, null, or an already-parsed object.
Can a JSON repair tool fix this error?
No. There is no broken JSON to repair; the input was the word "undefined". The fix is in your code: validate the value before parsing.
Related reading: Why Is My JSON Invalid? The 7 Syntax Errors That Break JSON.parse · "Unexpected End of JSON Input": Empty Body, Cut-Off Payload, or Unclosed Bracket · How to Fix "Converting Circular Structure to JSON" in JavaScript · NDJSON Broke My Parser: How to Fix JSON Lines Errors
One JSON gotcha a week, no fluff: Subscribe to the newsletter.