How browsers word it
| Chrome and Node (V8), newer | "undefined" is not valid JSON |
|---|---|
| Chrome and Node, older | Unexpected token u in JSON at position 0 |
| Firefox | JSON.parse: unexpected character at line 1 column 1 of the JSON data |
| Safari | JSON Parse error: Unrecognized token 'u' |
Common causes
The value was undefined
A variable that was never set, a missing property or a function without a return value gives undefined. JSON.parse converts it to the text undefined.
Input
undefined
JSON Formatter says
"undefined" is not valid JSON
An object was turned into text the wrong way
Joining an object into a string gives [object Object], not JSON. Use JSON.stringify to write it and JSON.parse to read it back.
Input
[object Object]
JSON Formatter says
Unexpected word "object". Strings need double quotes; the only bare words are true, false and null
A JavaScript value that JSON does not have
NaN, Infinity and undefined exist in JavaScript but not in JSON. The only bare words JSON allows are true, false and null.
Input
NaN
JSON Formatter says
"NaN" is not valid JSON
How to fix it
- Log the exact value you pass to JSON.parse, and its type.
- For localStorage.getItem, check for null before parsing, and write values with JSON.stringify.
- Give the parse a fallback: parse only when the value is a non-empty string.
- Paste the text into the JSON formatter to see what is wrong with it.