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"}
Code #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
Code #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
Recommended Posts:
- JavaScript | typedArray.name with Examples
- JavaScript | typedArray.from() with Examples
- JavaScript | typedArray.of() with Examples
- JavaScript | weakMap.has() with Examples
- JavaScript | weakSet.has() with Examples
- JavaScript | weakSet.add() with Examples
- JavaScript | weakMap.set() with Examples
- JavaScript | unescape() with examples
- JavaScript | addEventListener() with Examples
- JavaScript | console.log() with Examples
- JavaScript | getPrototypeOf() with Examples
- JavaScript | _.reduceRight() with Examples
- Javascript | _.reduce() with Examples
- JavaScript | typedArray.map() with Examples
- JavaScript | typedArray.every() with Examples
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.


