Skip to content
Related Articles

Related Articles

Improve Article

JavaScript JSON stringify() Method

  • Last Updated : 07 Jul, 2020
Geek Week

Below is the example of the JSON stringify() Method.

  • Example:




    <script>
        const value = {
            Company: "GeeksforGeeks",
            Estd: 2009,
            location: "Noida"
        };
        const result = JSON.stringify(value);
        document.write("value of result = " + result + "<br>");
    </script>                    
  • Output:
    value of result = {"Company":"GeeksforGeeks",
                       "Estd":2009,
                       "location":"Noida"}

The JSON.stringify() method 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 into a database or for sending the data to an API or web server. The data has to be in the form of the 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 which 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 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 number or a string if it is a number than the specified number of spaces indented to the final string and if it is a string then that string is (upto 10 characters) used for indentation.

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



Below example illustrate the JSON signify() method in JavaScript:

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

More example codes for the above method are as follows:
Program 1: In the below code, JavaScript object is being passed as a value in the function to convert it into a string.




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

Output:

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

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




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

Output:

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

Supported browsers:

  • Chrome 4.0
  • Firefox 3.5
  • Opera 11.0
  • Internet Explorer 8.0
  • Safari 4.0

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :