JavaScript

How to Convert JavaScript Object to JSON

Converting JavaScript Object to JSON is useful for having a mode of communication so that each programming language can cope up with every character accurately. Moreover, this type of conversion enables the data to be transferred between various programming languages in a format to build understanding. On the other hand, we cannot use a JavaScript object directly in PHP or C++; because each language has a different representation of an object.

This write-up will guide you about converting JavaScript Object to JSON.

How to Convert JavaScript Object to JSON?

JavaScript object can be converted into JSON using two simple methods:

We will now go through each of the mentioned approaches one by one!

Method 1: Convert JavaScript Object to JSON Using JSON.stringify() Method

The “stringify()” method is utilized to convert a JavaScript value to a “JSON” by accepting the value that needs to be converted into JSON as an “argument”.

To convert the JavaScript Objects to JSON format using the “stringify()” method, you have to follow the below-given syntax.

Syntax

JSON.stringify(value)

Here, “value” represents the JavaScript object that will be converted into JSON.

Look at the below-given example.

Example

Firstly, we will create a null array to contain the objects and their corresponding values in it. Now, we will create two properties, “name” and “id”, and assign them the following values:

var obj= {};

obj.name="Harry"

obj.id= 1

Then, we will perform the required functionality of converting JavaScript Object to JSON using the “stringify()” method. This will be done by specifying “obj” as an argument and displaying the resultant JSON value:

var json = JSON.stringify(obj);

console.log(json);

Execution of the above code will result in:

Method 2: Convert JavaScript Object to JSON Using Object.keys() Method

Object.keys()” is a JavaScript method that accepts the key of an object and returns its corresponding value. You can apply this method to convert the created objects to JSON and store them in an array. Moreover, we will also add “{}” for accumulating the values in an array.

For converting the JavaScript Object to JSON using the Object.keys() method, you have to use the following syntax:

Syntax

Object.keys(obj)

Here, “obj” refers to the keys for which the Object.keys() method will fetch the values.

Here is an example for the demonstration.

Example

We will apply the “Object.keys()” method on the already created object and place “obj” in an argument that will access the values of its keys. Moreover, we have also added “{” to accumulate the values in an array form:

var keys= Object.keys(obj);

var json= "{";

In the next step, we will use a for loop for iterating along the declared objects in an array. Here, keys[i] refers to the objects “name” and “id”, and obj[keys[i] refers to the values placed in the corresponding objects.

The “json” variable is added to it as this statement will be executed two times such that in the first iteration, it will fetch the value of the “name” key, and in the next iteration, it performs the same operation for “id”. The resultant value will be concatenated with the created object using the “+” operator. Also, we will convert the objects and their values into string values using “$”:

for (let i= 0; i < keys.length; i++) {

json= json + `"${keys[i]}":"${obj[keys[i]]}",`;

}

Finally, we will add an ending “}” bracket and add it to the array which will result in the proper accumulation of the object and their corresponding values in an array. Then, we will display the converted JSON string values on the console:

json= json + "}";

console.log(json);

The resultant output in this case will be:

We have compiled all the convenient methods related to converting JavaScript Object to JSON. You can use any of the above methods according to your requirements.

Conclusion

To Convert JavaScript Object to JSON, you can apply the “JSON.stringify()” method by placing the variable name in arguments for referring to the objects and their values. Moreover, you can also utilize the “object.keys()” method for the specified conversion and returning array of keys and their corresponding values. This article guided about converting JavaScript Object to JSON.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.