JavaScript | JSON.stringify() with Examples

The JSON.stringify() is an inbuilt function in JSON which allows us to take a JavaScript object or Array and 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 form of the strings. This conversion of an object to a string can be easily done with the help of the JSON.stringify() function.
Syntax:

JSON.stringify(value, replacer, space)

Parameters: It accepts three parameters which are specified 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.



Example:

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

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

JavaScript code to show the working of JSON.stringify() function:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.