JavaScript

How to convert JavaScript Object into JSON string format

If you are using any web application, then there exist chances that you are utilizing the JavaScript Object Notation or JSON to organize, store, and send data between the server and that specific application. With the help of the JSON.stringify() method, you can easily convert a JavaScript object into a string that will have a valid JSON format. It is typically used for generating a ready-made string that can be delivered to a server.

This write-up will explain JSON.stringify() method in JavaScript. We will also demonstrate the examples related to using JSON.stringify() method with replacer array, replacer function, and space parameter in this article. So, let’s start!

What is JSON.stringify() Method

As a JavaScript developer, you may need to serialize the data to string to store in the application’s database or send it to any web server or an API. If you need to send any specific data to a web server, it must be in string format.

Syntax of JSON.stringify() Method

JSON.stringify(value, replacer, space);

You can see from the above-given syntax that the JSON.stringify() method has there parameters: “value”, “replacer”, and “space”:

  • The first parameter, “value” represents the “object” that we will convert into a string.
  • The second parameter, “replacer” represents an array or any modifying function that can be utilized as a filter for the JSON.stringify() method.
  • Lastly, the “space” parameter controls spaces in the final generated string.

The “replacer” and “space” parameters are optional, whereas you must pass any object as “value” to the JSON.stringify() method so that it can return a string.

Example: JSON.stringify() Method in JavaScript

In the below-given example, we will utilize the JSON.stringify() method for converting an object in a string in JavaScript. For this purpose, firstly we will create a JavaScript object “obj” and add some key-value pairs for it:

var obj = { "name":"Alex", "age":25, "city":"Paris"};

Next, we will pass “obj” to the JSON.stringify() method and the returned string will be stored in “JSON”:

var JSON = JSON.stringify(obj);

After invoking the JSON.stringify() method, the keys added in our “obj” JavaScript object are converted into a string, however, the specified method processed their values based on their type:

console.log(JSON);

You can utilize any online coding sandbox or your favorite code editor for executing the provided JavaScript program; however, we will use the Visual Studio Code:

The output of the above program shows that the JSON.stringify() method has successfully converted the added object into a string:

JSON.stringify() Method with replacer

As mentioned above, “replacer” is an argument passed to the JSON.stringify() method for making changes into a JavaScript object before its conversion into a string. The “replacer” parameter of JSON.stringify() method can be an array or a function. We will provide you examples related to both cases.

Example: JSON.stringify() Method with replacer function

To define a replacer function, first, you have to specify “key” and “value” as its arguments. After that, you can add any conditional statements in its body and set this function to return a “value”.

In this example, we will try to convert the value of an object into uppercase letters before converting it into a string. To do so, we will create a “obj” JavaScript object having three key-values pairs:

var obj = { "name":"Alex", "age":"20", "city":"Paris"};

Then, we will invoke the JSON.stringify() method and add the replacer function to convert the value of “city” key into uppercase letter. After performing this operation, the JSON.stringify() method will convert “obj” to string and store the returned value in “text”:

var text = JSON.stringify(obj, function (key, value) {

if (key == "city") {

return value.toUpperCase();

} else {

return value;

}

});

Lastly, we will display the converted string using the console.log() method:

console.log(text);

Check out the below-given output; the value of the “city” key is now in uppercase letters:

Example: JSON.stringify() Method with replacer array

Now, we will pass a replacer array into the JSON.stringify() method as an argument. For this purpose, we will create an “obj” JavaScript object with the following “key: value” pairs:

var obj = { "name":"Alex", "age":25, "city":"Paris"};

Next, we will pass the “obj” as a value and “[‘name’, ‘age’]” as an array. Upon doing so, the JSON.stringify() method will only convert the keys added into the passed array. The value returned by the invoked method will be stored in “JSON”:

var JSON = JSON.stringify(obj, ['name', 'age']);

console.log(JSON);

Here is the output we got from by passing replacer array in our JSON.stringify() method:

JSON.stringify() Method with space

space” is another optional parameter added in the “JSON.stringify()” method for controlling the presentation or display of the converted string. A “number” is added as space that represents the number of blank spaces you want to place at the start of a line where the string outputs start.

Example: JSON.stringify() Method with space

In our JavaScript program, we will invoke the JSON.stringify() method while adding space parameter. To do so, we will create a JavaScript object named “obj”, having the following three “key: value” pairs:

var obj = { "name":"Alex", "age":"25", "city":"Paris"};

After that, we will utilize the JSON.stringify() method for converting our JavaScript object “obj” to a “text” string. Note that we have added “4” as a space parameter that represents the number of spaces before the string starts:

var text = JSON.stringify(obj, null, 4);

console.log(text);

As you can see from the above-given output that four spaces are successfully added before each“key: value” pair.

Conclusion


The JSON.stringify() method converts a JSON object into a string. In the JSON.stringify() method, you can specify various parameters such as replacer and space to change any key-value or control the spaces in the output. This write-up explained JSON.stringify() method in JavaScript with the demonstration of the examples using replacer array, replacer function, and space.

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.