Fix the “[object Object] is not valid JSON” error

Why JSON.parse fails on the text [object Object], how to find where an object was converted to a string, and the fixes for storage, fetch and double parsing.

This error means JSON.parse received the literal 15-character text "[object Object]". Before the value reached the parser, a JavaScript object was converted to a string the default way. The parser is behaving correctly, and the original data was lost at the point of conversion. The fix is to find where that conversion happened.

What the message actually means

When JavaScript needs a string from a plain object that has no custom toString method, it falls back to Object.prototype.toString, which returns "[object Object]". This happens with String(obj), with string concatenation, with template literals, and with any API that coerces its argument to a string. Examples include localStorage.setItem, a fetch request body and URL parameters. JSON.parse also converts a non-string argument to a string first, so JSON.parse(obj) tries to parse "[object Object]".

The parse fails at the second character. An opening square bracket is a valid start for a JSON array. The letter o that follows is not a valid JSON value, so the parser stops there.

const user = { name: "Ada" };

String(user);            // "[object Object]"
`${user}`;               // "[object Object]"
"data: " + user;         // "data: [object Object]"

JSON.parse(user);        // SyntaxError: parses the text "[object Object]"
JSON.stringify(user);    // '{"name":"Ada"}'

The wording of the error depends on the engine and its version. Recent V8-based runtimes such as Chrome, Edge and Node.js report something like "[object Object]" is not valid JSON. Older versions said Unexpected token o in JSON at position 1. Other engines describe an unexpected character at line 1, column 2. All of these point to the same cause. This article does not pin down which release introduced which wording, so check the message your own runtime produces.

Stored text versus what the console shows

When you call console.log(user), the console shows an expandable view of the live object. That view is only a presentation of the object. After the object has been coerced to a string, the stored value is literally the characters [object Object], and no property names or values remain in it. The conversion is lossy and cannot be reversed by any parsing. If "[object Object]" was written to Web Storage, a database or a log, the original data has to be recovered from another source.

Where the conversion usually happens

Cause | Typical code | How to confirm | Fix
Parsing data that is already an object | JSON.parse(response.data) with axios, or JSON.parse on the result of response.json() | typeof value is "object" just before the parse call | Remove the extra JSON.parse
Saving an object to Web Storage without stringifying | localStorage.setItem("user", user) | The browser's storage panel shows the value as [object Object] | Save JSON.stringify(user) and clear the stale entry
Sending an object as a fetch body | fetch(url, { method: "POST", body: data }) | The Network tab request payload shows [object Object] | Use body: JSON.stringify(data) with a Content-Type of application/json
Building JSON by string concatenation | '{"user": ' + user + '}' | Log the assembled string before it is sent or parsed | Build the whole object first, then stringify it once
Parsing message data that arrives as an object | JSON.parse(event.data) after postMessage sent an object | typeof event.data is "object" | Use event.data directly, or send a string on purpose

Checks that tell the causes apart

  • Log typeof value, and the value itself, immediately before the JSON.parse call. If it is "object", the data was already parsed and the extra parse should go.
  • If it is a string containing [object Object], the conversion happened earlier. Trace back to where the value was written: the storage entry, the network payload or the variable that assembled it.
  • If a server reports the error from its JSON body parser, open the request in the browser's Network tab. A payload of [object Object] means the client sent an unserialized object.
  • Search the code for setItem calls, fetch body options and string concatenation that involve variables holding objects.

There is one fetch detail that confuses diagnosis. When fetch receives a plain object as its body, it converts the object to the string [object Object] and labels the request as plain text. A server parser that only reads JSON content may skip such a request and leave the body empty instead of throwing an error. The parse error appears when the client also sets Content-Type to application/json by hand. As a result, an empty request body on the server and this error can come from the same client mistake.

Targeted corrections

// Already parsed: use the result directly
const res = await fetch("/api/user");
const user = await res.json();   // no JSON.parse needed

// Storage: stringify on write, parse on read
localStorage.setItem("user", JSON.stringify(user));
const saved = JSON.parse(localStorage.getItem("user") ?? "null");

// Request body
await fetch("/api/user", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify(user),
});

Fixing the write path does not remove values that were already saved. Users' browsers may still hold [object Object] entries from before the fix. The read path should catch the parse failure, discard the unreadable value and fall back to a default.

function readStored(key, fallback) {
  const raw = localStorage.getItem(key);
  if (raw === null) return fallback;
  try {
    return JSON.parse(raw);
  } catch {
    localStorage.removeItem(key);   // discard unreadable value
    return fallback;
  }
}

A helper that parses only when the input is a string can make the error go away, but it also hides a double-parse bug. Reserve it for boundaries where the input really can arrive in either form. Everywhere else, settle on one documented format for each value.

Does JSON.stringify preserve what you need?

The standard fix is to serialize with JSON.stringify, but JSON cannot represent every JavaScript value. Before relying on a round trip, check whether your data contains any of the following:

  • Functions, undefined and Symbol values are dropped from objects and become null inside arrays.
  • Date objects become ISO strings, and JSON.parse returns them as strings rather than Dates.
  • Map and Set instances serialize as empty objects.
  • NaN and Infinity become null.
  • BigInt values cause a TypeError.
  • Circular references cause a TypeError.
  • Class instances come back as plain objects without their prototype or methods.

If any of these matter, convert the values explicitly before serializing. Alternatively, pass a replacer function to JSON.stringify and a matching reviver to JSON.parse.

Readability after copying or sharing

JSON.stringify(value, null, 2) produces indented output that is still valid JSON after you copy it. That makes it suitable for bug reports and for pasting into a validator. Compact output, with no spacing argument, is better for storage and network traffic. Both forms parse to the same result.

For logs that only people will read, plain inspection output is often better than JSON. Passing the object as a separate argument, as in console.log("user:", user), keeps it inspectable, while concatenating it into the message prints [object Object]. In Node.js, util.inspect can display values that JSON cannot, such as circular structures and Maps. Its output is not JSON, though, and should never be parsed back.

In some browsers, an object expanded in the console shows its state at the moment you expand it, not the moment it was logged. When you need an exact snapshot to copy, log the stringified value instead. If the output is meant for end users rather than programs, show formatted fields or a plain-language message instead of raw JSON.

Recommendation

Treat the error as a sign that an object was turned into text somewhere upstream. Find that spot and serialize there with JSON.stringify. Then parse exactly once, at the point where the text comes back in. This keeps the data intact for plain objects, arrays, strings, numbers and booleans. The main tradeoff is that JSON drops or changes values such as Dates, Maps, BigInts and functions, so data containing them needs explicit conversion. For messages and logs that only people read, plain console output or a short text summary serves better than JSON.

Limits of this guide: no runtime-specific test results were available for it. Exact error wording and the versions that introduced each message were not verified, and the behavior of particular server frameworks should be confirmed against their own documentation.