Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript JSON stringify() Method

Improve Article
Save Article
  • Last Updated : 09 Jan, 2023
Improve Article
Save Article

The JSON.stringify() method in Javascript is used to create a JSON string out of it. While developing an application using JavaScript, many times it is needed to serialize the data to strings for storing the data in a database or for sending the data to an API or web server. The data has to be in the form of strings. This conversion of an object to a string can be easily done with the help of the JSON.stringify() method.

Syntax:

JSON.stringify(value, replacer, space);

Parameters: This method accepts three parameters as mentioned above and described below:

  • value: It is the value that is to be converted into a JSON string.
  • replacer: It is an optional parameter. This parameter value can be an altering function or an array used as a selected filter for the stringify. If the value is empty or null then all properties of an object are included in a string.
  • space: It is also an optional parameter. This argument is used to control spacing in the final string generated using JSON.stringify() function. It can be a number or a string if it is a number then the specified number of spaces are indented to the final string and if it is a string then that string is (up to 10 characters) used for indentation.

Return Value: It returns a string for a given value.

Example: The below example illustrates the JSON signify() method in JavaScript:

var value = { name: "Logan", age: 21, location: "London" };
var result = JSON.stringify(value);

Output:
{"name":"Logan", "age":21, "location":"London"}

Example 1: Below is an example of the JSON stringify() Method. 

Javascript




<script>
    const value = {
        Company: "GeeksforGeeks",
        Estd: 2009,
        location: "Noida"
    };
    const result = JSON.stringify(value);
    console.log("value of result = " + result);
</script>

Output:

value of result = {"Company":"GeeksforGeeks",
                   "Estd":2009,
                   "location":"Noida"}

Example 2: In the below code, the JavaScript object is being passed as a value in the function to convert it into a string.

Javascript




<script>
    var value = {
        name: "Logan",
        age: 21,
        location: "London"
    };
    var result = JSON.stringify(value);
    console.log("value of result = " + result);
    console.log("type of result = " + typeof result);
</script>

Output:

value of result = {"name":"Logan", "age":21, "location":"London"}
type of result = string

Example 3: In the below code will look over as it is helpful to convert it into a string without doing changes in the original object so helpful for the nested objects to changes without reflecting in the original object.

Javascript




<script>
    let obj = {
    name: "GFG",
    add : {
        country: "India",
        state : {
            code : "JS",
            topic : "stringify"
        }
    }
    }
     
    let obj2 = JSON.parse(JSON.stringify(obj));
    obj2.add.state.topic  = "stringify json object";
    console.log(obj);
    console.log(obj2);
</script>

Output:

JavaScript JSON stringify() Method

JavaScript JSON stringify() Method

Example 4: In the below code, the JavaScript array can be passed as a value in the function to convert it into a string.

Javascript




<script>
    var value = ["Logan", 21, "Peter", 24];
    var result = JSON.stringify(value);
    console.log("value of result = " + result);
    console.log("type of result = " + typeof result);
</script>

Output:

value of result = ["Logan", 21, "Peter", 24]
type of result = string

We have a complete list of Javascript JSON methods, to check those please go through Javascript JSON Complete Reference article.

Supported browsers:

  • Chrome 4.0
  • Firefox 3.5
  • Microsoft Edge 12.0
  • Opera 11.0
  • Internet Explorer 8.0
  • Safari 4.0

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!