English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
JSON is used for data exchange with web servers. When sending data to a web server, the data must be a string.
The JSON.stringify() method converts JavaScript objects or values to JSON strings.
If the method optionally replaces valuesSubstituteSpecified or optional if only including specified properties functionSubstituteSpecify an array.
Syntax:
JSON.stringify(value, replacer)
The first parameter specifies the value to be converted to a JSON string.
An optional second parameter specifies a function that can change the behavior of the stringification process.
Suppose we have the following object in JavaScript:
var myObj = {name: "Seagull", age: 22, city: "New Delhi"};
Using the JSON.stringify() method, we can convert a JavaScript object to a JSON string:
var myJSON = JSON.stringify(myObj);Test and see‹/›
Suppose we have the following array in JavaScript:
var myArr = ["Seagull", "Cynthia", "Tarush"];
Using the JSON.stringify() method, we can convert a JavaScript array to a JSON string:
var myJSON = JSON.stringify(myArr);Test and see‹/›
Date objects are not allowed in JSON.
The JSON.stringify() method will convert any date object to a string.
var myObj = { name: "Seagull", today: new Date(), city: "New Delhi" }; var myJSON = JSON.stringify(myObj); document.getElementById("output").innerHTML = myJSON;Test and see‹/›
Note: Converting a string to a local object is calledParsing, while converting a local object to a format that can be transmitted over the networkStringis calledStringification.