Unexpected Token in JSON at Position 0: What It Means and How to Fix It
Unexpected token <, o, u or ' in JSON means JSON.parse received something that is not JSON: usually an HTML error page, an object instead of a string, or undefined. Find which one in a minute.
Check the text in the validatorRead the token: it tells you what you received
The parser reports the first character it could not accept. Older Chrome and Node.js versions say Unexpected token < in JSON at position 0; newer ones quote the start of the input, for example Unexpected token '<', "<!DOCTYPE "... is not valid JSON. Firefox says JSON.parse: unexpected character at line 1 column 1. They all mean the same thing.
Position 0 means the very first character was wrong, so the whole input is something other than JSON. A later position means the input started as JSON and broke partway through; for that case see fix invalid JSON.
- < at position 0: you received HTML, such as an error page, a login page or your app's index.html.
- o at position 1: you parsed the string "[object Object]", which means you passed an object, not text.
- u at position 0, or "undefined" is not valid JSON: you parsed undefined, such as a missing localStorage key or an empty variable.
- ' (single quote): the text uses JavaScript or Python quoting. JSON requires double quotes.
- An invisible character at position 0: the file starts with a byte order mark (BOM).
Unexpected token <: the server sent HTML
This is the most common case. The request reached a page instead of your API: a 404 or 500 error page, a proxy error, an expired session redirecting to a login form, or a single-page app that serves index.html for every unknown path. The status code may even be 200.
Log the raw response before parsing it. The first few hundred characters almost always show the cause, such as a page title or a framework error message.
const res = await fetch("/api/users");
const text = await res.text();
if (!res.ok || !res.headers.get("content-type")?.includes("application/json")) {
console.error(res.status, text.slice(0, 300));
throw new Error(`Expected JSON, got ${res.status}`);
}
const data = JSON.parse(text);Unexpected token o: you parsed an object
JSON.parse converts its argument to a string first. An object becomes "[object Object]", the parser accepts the opening [ as an array and then fails on the o at position 1. The value was already parsed, often because a library such as axios or response.json() did it for you.
Remove the extra JSON.parse call. Use typeof value === "string" if the same code receives both shapes.
const res = await axios.get("/api/users");
const users = res.data; // already an object
// JSON.parse(res.data) -> Unexpected token o in JSON at position 1"undefined" is not valid JSON
Newer engines word this case directly. The input was the text undefined. Usually localStorage.setItem was once given an undefined value, which stores the string "undefined", or a missing variable or property was passed straight to JSON.parse. A key that was never set returns null, and JSON.parse(null) is fine.
Guard the read and store only values produced by JSON.stringify.
const raw = localStorage.getItem("settings");
const settings = raw && raw !== "undefined" ? JSON.parse(raw) : {};
localStorage.setItem("settings", JSON.stringify(settings ?? {}));Single quotes, BOMs and other look-alikes
Text such as {'name': 'Ada'} is a JavaScript or Python literal, not JSON. Fix the producer to serialize with JSON.stringify or json.dumps rather than string formatting. For a one-off file, the repair button in the validator converts single quotes.
A file saved by some Windows editors starts with the bytes EF BB BF. Strip it before parsing with text.replace(/^\uFEFF/, "").
Put the example to work
Use your own sample, check the result, and copy or download it. The tool processes your data locally in your browser.
Check the text in the validatorReference documentation