JavaScript

JSON.parse(): Fixing ‘SyntaxError: “[object Object]” is not valid JSON’

Advertisements

SyntaxError: “[object Object]” is not valid JSON: usually happens when the value being passed to JSON.parse is not a string value.

Let’s see how we can fix it.

Scenario 1: The value is already an object

const object = JSON.parse({ website: "https://wisdomgeek.com" });
// Uncaught SyntaxError: "[object Object]" is not valid JSON
JavaScript

Since the value being passed to JSON.parse is already an object, we don’t need to parse it again. It can simply be a definition of the object.

const object = { website: "https://wisdomgeek.com" };
JavaScript

Scenario 2: The object needs to be converted to a string using JSON.stringify first

There might be cases where we want to parse an object value. In those cases, to avoid the ‘SyntaxError: “[object Object]” is not valid JSON’ error, we need to convert the object to a string first.

const object = JSON.parse(JSON.stringify({ website: "https://wisdomgeek.com" }));
JavaScript

The above example is very naive and should not be used. But it can be helpful if there is something else that is happening in between the conversion, like converting the object to a string to store it in local storage and then retrieving it later.

Note: This is also a common technique to create a deep copy of a JavaScript object.

Troubleshooting other scenarios

If you are still stuck, you might want to check the type of variable being passed to JSON.parse:

// let us assume this is initialized somewhere else
const object = { website: "https://wisdomgeek.com" };


const parsedObject = JSON.parse(object);
// Uncaught SyntaxError: "[object Object]" is not valid JSON

console.log(typeof object); // 👉️ "object"
// we need to pass a string
JavaScript

This will also help you if you forgot to wait for a promise to be resolved when fetching JSON from third-party sources.

const response = await fetch("https://dummyjson.com/products/1");
const text = response.text(); 
const parsedData = JSON.parse(text);

// Uncaught SyntaxError: "[object Object]" is not valid JSON
JavaScript

If we check the type of jsonData:

console.log(typeof jsonData);
// object
JavaScript

We can identify the issue and add a promise.then or an await to fix it.

const response = await fetch("https://dummyjson.com/products/1");
const text = await response.text(); 
const parsedData = JSON.parse(text);

// {id: "1", .... }
JavaScript

If the value being passed is a string and we still are getting an error, the string might not be a valid JSON:

// let us assume this is initialized somewhere else
const objectString = '{ "https://wisdomgeek.com" }';

const parsedObject = JSON.parse(object);
// Uncaught SyntaxError: Expected ':' after property name in JSON at position 27
JavaScript

In this case, you need to fix the string being passed into the JSON.parse method. You can use a JSON validator like this one to check if the JSON is valid or not.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria
Tags: javascript

Recent Posts

Remapping keyboard keys to avoid Carpal Tunnel

I am terrible at optimizing my keyboard layout for anything. But off lately, my little…

1 month ago

Fixing cookies are blocked for a website with shields down on Brave

I recently switched completely to the Brave browser and have set ad blocking to aggressive…

5 months ago

Generating a QR code using Node.js

I was preparing a slide deck for a hackathon and decided to put in a…

6 months ago

How to clear the global npx cache

I have been using npx a lot lately, especially whenever I want to use a…

6 months ago

Copy/Pasting output from the terminal

Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…

7 months ago

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

8 months ago
Advertisements