JSON Errors / JavaScript
SyntaxError: "..." is not valid JSONHow to Fix: SyntaxError: "..." is not valid JSON
This is the modern V8 (Chrome 104+, Node 18+) wording for JSON.parse() failures - the same failures that older versions reported as "Unexpected token ... at position ...". The message now quotes a snippet of what you tried to parse, which is a gift: the quoted text shows you exactly what the parser saw.
Read the quoted part carefully. If it shows "[object Object]" you passed an object instead of a string; if it starts with "<" you received HTML; if it shows "undefined" your variable was never set.
Common causes
1. The quote shows "[object Object]"
You passed an already-parsed object to JSON.parse(). Use the value directly or stringify it first - see the dedicated guide for this variant.
SyntaxError: "[object Object]" is not valid JSON2. The quote shows "undefined"
The variable you parsed was the string "undefined" or the value undefined - a missing localStorage key or an unset field serialized somewhere upstream.
JSON.parse(undefined) // "undefined" is not valid JSON3. The quote shows HTML or plain text
The string genuinely is not JSON: an HTML error page, a plain-text message like "Too Many Requests", or a truncated document.
How to fix it
function safeParse(raw) {
if (typeof raw !== "string" || raw.trim() === "") {
console.warn("safeParse: not a string:", typeof raw, raw);
return null;
}
try {
return JSON.parse(raw);
} catch (e) {
console.error("safeParse failed. First 200 chars:", raw.slice(0, 200));
throw e;
}
}Frequently asked questions
Why did the error message change from "Unexpected token"?
V8 improved its JSON error messages in 2022 (Chrome 104, Node 18) to include a snippet of the invalid input and, for longer inputs, position context. Same errors, more useful wording. Firefox and Safari still use their own phrasings.
The quoted snippet is my whole API response. Is that a bug?
No - V8 quotes short inputs entirely. It means your response really is that short string (often an error message like "Unauthorized"), which is why it is not parseable as JSON.
How do I handle this gracefully in production?
Wrap JSON.parse in try/catch at every boundary where data enters your system (network, storage, user input), log the raw value on failure, and fall back to a default. Never let a parse error from external data crash the app.
Last updated:
Related JSON errors
You might also need
Free and in-browser, like everything on JSON Console. Browse all tools →