"[object Object]" is not valid JSON
JSON.parse only works on text. Hand it an object and JavaScript first converts it to a string, and the string it gets is the useless [object Object]. That’s what the error is quoting back at you. "undefined" is not valid JSON is the same mistake with undefined.
Quick fix
- Add
console.log(typeof value, value)just before theJSON.parsecall. - If it says
"object", the data has already been parsed. Remove theJSON.parseand use the object directly. - If it says
"undefined", trace back to where the value should have been set.
Parsing data that’s already been parsed
This is the most common cause. response.json() doesn’t return JSON text; it reads the text and gives you back the parsed object. Axios’s response.data does the same. So if you then call JSON.parse on the result, you’re parsing twice:
const response = await fetch("/api/user");
const user = await response.json(); // already an object
JSON.parse(user); // "[object Object]" is not valid JSON
console.log(user.name); // just use itSaving or sending an object without stringifying it
With localStorage, the error shows up later, when you read the data back. It only stores text, so saving an object stores the string [object Object] and the data is lost. Stringify when you save, and clear out the bad entry once:
// Wrong: stores the text "[object Object]"
localStorage.setItem("user", user);
// Right
localStorage.setItem("user", JSON.stringify(user));
const saved = JSON.parse(localStorage.getItem("user") ?? "null");The same thing happens if you pass an object as a request body. The server receives the text [object Object], and it’s the server’s JSON parser that fails:
await fetch("/api/user", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(user), // not body: user
});Where the undefined comes from
Usually it’s a property that doesn’t exist, like a misspelled response.body, or a function that forgot to return anything. A missing localStorage key won’t cause it: getItem returns null, and JSON.parse(null) returns null without an error. If a value might legitimately be missing, check before you parse: text ? JSON.parse(text) : null.
The same error elsewhere
| Chrome, Edge, Node.js 20+ | "[object Object]" is not valid JSON |
|---|---|
| Chrome, Edge, Node.js 20+ | "undefined" is not valid JSON |
| Older Chrome, Node.js 18 | Unexpected token o in JSON at position 1 |
| Older Chrome, Node.js 18 | Unexpected token u in JSON at position 0 |