Unexpected token u in JSON at position 0
"undefined" is not valid JSON (Node 20+: “"undefined" is not valid JSON” — older engines show: “Unexpected token u in JSON at position 0”)
This error occurs when you call JSON.parse() with the JavaScript value undefined instead of a string. Because undefined is not a string, the engine coerces it to the string "undefined", which is not valid JSON. The classic Chrome/Node 18 message is "Unexpected token u in JSON at position 0"; Node 20+ and modern V8 report “\"undefined\" is not valid JSON” instead. Both phrasings are searched heavily and refer to the exact same root cause.
API or fetch response was not checked before parsing
The most common trigger is calling JSON.parse() on the result of response.text() or a variable that might be undefined when the network request failed or returned no body. Always check that the value is a non-empty string before parsing.
Invalid
const data = JSON.parse(response) // response is undefined
Valid
if (response) {
const data = JSON.parse(response)
}Missing object property used as input
Reading a property that does not exist on an object returns undefined. If you pass that directly to JSON.parse, this error fires. Guard with optional chaining and a fallback.
Invalid
const raw = config.payload // property missing → undefined const parsed = JSON.parse(raw) // throws
Valid
const raw = config.payload ?? "" const parsed = raw ? JSON.parse(raw) : null
localStorage.getItem() returned null
When a localStorage key does not exist, getItem() returns null. Passing null to JSON.parse coerces it to the string "null", which actually parses fine — but passing a missing variable (undefined) does not.
Invalid
const saved = localStorage.getItem("prefs") // returns null if missing
JSON.parse(saved) // "null" parses to null — but be carefulValid
const saved = localStorage.getItem("prefs")
const prefs = saved ? JSON.parse(saved) : {}Function called before asynchronous data arrives
In React and other frameworks, parsing data in a synchronous context before an async fetch completes leaves the variable undefined. Move the parse inside the async callback or await the promise.
Invalid
let raw
fetchData().then(r => { raw = r })
JSON.parse(raw) // raw still undefined hereValid
const raw = await fetchData() JSON.parse(raw)
Fix this error in seconds
Paste your JSON in the free validator to find the exact line and column where the error occurs.
Open JSON Validator →Frequently Asked Questions
Why does the error say position 0 if undefined has no characters?
The engine coerces undefined to the string "undefined" before attempting to parse it. The parser sees the character "u" at index 0, which is not a valid JSON start character (JSON must start with {, [, ", a digit, or the literals true/false/null), so it reports the failure at position 0.
How do I prevent this error in a fetch-based API call?
Check response.ok before calling response.json() or JSON.parse(await response.text()). Additionally, wrap the call in a try/catch so that unexpected non-JSON responses (like an HTML error page) are handled gracefully rather than crashing your app.