How to Resolve "SyntaxError: '[object Object]' is not valid JSON" Error in JavaScript
The SyntaxError: "[object Object]" is not valid JSON (and its common variant SyntaxError: Unexpected token 'o' in JSON at position 1) is a frequent error in JavaScript that occurs when you misuse the JSON.parse() method. It's a clear signal that you are trying to parse something that is already a JavaScript object, not a JSON string.
This guide will explain the fundamental difference between a JSON string and a JavaScript object, show you why this error happens, and teach you how to fix it.
The Core Problem: JSON String vs. JavaScript Object
It is crucial to understand the difference between these two concepts:
- JavaScript Object: This is a native data type in JavaScript. It's a collection of key-value pairs that exists in memory and can be accessed directly.
const myObject = { name: 'Alice', id: 1 }; - JSON String: This is a string representation of data, following the strict JSON (JavaScript Object Notation) format. It is used for storing or transmitting data.
const myJsonString = '{"name":"Alice","id":1}';
The purpose of JSON.parse() is to take a JSON string and convert it into a JavaScript object. The error occurs when you give it something that is already an object.
How the Error Occurs (And What [object Object] Means)
When you pass a non-string value (like a real object) to JSON.parse(), it first tries to convert that value to a string. The default string representation of a JavaScript object is the literal string "[object Object]".
const myObject = { name: 'Alice' };
// PROBLEM: We are passing an object, not a string, to JSON.parse().
// 1. JavaScript first converts `myObject` to the string "[object Object]".
// 2. Then, `JSON.parse()` tries to parse this string.
JSON.parse(myObject);
The parser then attempts to read "[object Object]". The first character it sees is [, which it thinks might be the start of an array. But the next character is o, which is an "unexpected token," causing the error.
Error Output:
Uncaught SyntaxError: Unexpected token 'o' in JSON at position 1
// Or in some environments:
// SyntaxError: "[object Object]" is not valid JSON
The Solution: Remove the Unnecessary JSON.parse() Call
The fix is almost always to simply remove the JSON.parse() call. If your data is already a JavaScript object, you don't need to parse it. This is very common when working with libraries like Axios or modern fetch wrappers that automatically parse the JSON response for you.
// A library like Axios or a fetch wrapper might give you this object directly.
const data = { name: 'Alice', id: 1 };
// If it's already an object, you can use it directly.
// No parsing needed!
console.log(data.name); // Output: 'Alice'
You can verify if your data is an object by using the typeof operator:
console.log(typeof data); // Output: 'object'
If typeof returns 'object' and not 'string', you do not need to call JSON.parse().
A Common Related Task: Converting an Object to a JSON String
If your goal is the reverse—to convert a JavaScript object into a JSON string for storage or transmission—you should use the JSON.stringify() method.
const myObject = { name: 'Alice', id: 1 };
// Convert the object to a JSON string.
const myJsonString = JSON.stringify(myObject);
console.log(myJsonString); // Output: '{"name":"Alice","id":1}'
console.log(typeof myJsonString); // Output: 'string'
Conclusion
The "[object Object]" is not valid JSON error is a clear sign that you are trying to parse data that has already been parsed.
To solve it:
- Check your data: If the variable is already a JavaScript object (use
typeofto verify), you do not need to parse it. Remove theJSON.parse()call. - Check your tools: Be aware that many modern HTTP clients (like Axios) and the
response.json()method of thefetchAPI automatically parse the JSON for you. You are likely trying to parse the result a second time. - If your goal is to create a JSON string from an object, use
JSON.stringify(), notJSON.parse().