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>chevron_rightfilter_none - 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
Recommended Posts:
- How to convert JSON string to array of JSON objects using JavaScript ?
- JavaScript JSON parse() Method
- Node.js | querystring.stringify() Method
- Javascript | JSON PHP
- JavaScript JSON
- JavaScript | JSON Arrays
- JavaScript | JSON HTML
- How to swap key and value of JSON element using JavaScript ?
- JavaScript | How to add an element to a JSON object?
- JavaScript | Convert an array to JSON
- JavaScript | Add new attribute to JSON object
- How to convert PHP array to JavaScript or JSON ?
- JavaScript | Remove a JSON attribute
- Deserializing a JSON into a JavaScript object
- How to convert JSON results into a date using JavaScript ?
- JavaScript SyntaxError - JSON.parse: bad parsing
- How to send a JSON object to a server using Javascript?
- How to pretty print JSON string in JavaScript ?
- Converting JSON text to JavaScript Object
- JavaScript | Check if a key exists inside a JSON object
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.


