JSON.stringify()
JSON is commonly used for exchanging data with the server.
When sending data to the server, it is usually in the form of a string.
We can use the JSON.stringify() method to convert a JavaScript object into a string.
Syntax
JSON.stringify(value[, replacer[, space]])
Parameter Description:
- value:
Required. The JavaScript value to be converted (typically an object or array).
- replacer:
Optional. A function or array used to transform the result.
If replacer is a function, JSON.stringify will call this function and pass in each member's key and value. The return value is used instead of the original value. If this function returns undefined, the member is excluded. The key for the root object is an empty string: "".
If replacer is an array, only members with keys in this array are converted. The order of conversion is the same as the order of the keys in the array. If the value parameter is also an array, the replacer array is ignored.
- space:
Optional. Adds indentation, white space, and line break characters to the return-value JSON text for readability purposes. If space is a number, the return-value text is indented with that number of spaces at each level. If space is greater than 10, text is indented 10 spaces. space can also be a non-numeric value, such as: \t.
JavaScript Object Conversion
For example, we send the following data to the server:
var obj = { "name":"tutorialpro", "alexa":10000, "site":"www.tutorialpro.org"};
We use the JSON.stringify() method to convert the above data into a string:
var myJSON = JSON.stringify(obj);
myJSON is a string.
We can send myJSON to the server:
Example
var obj = { "name":"tutorialpro", "alexa":10000, "site":"www.tutorialpro.org"};
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
JavaScript Array Conversion
We can also convert a JavaScript array into a JSON string:
Example
var arr = [ "Google", "tutorialpro", "Taobao", "Facebook" ];
var myJSON = JSON.stringify(arr);
myJSON is a string.
We can send myJSON to the server:
Example
var arr = [ "Google", "tutorialpro", "Taobao", "Facebook" ];
var myJSON = JSON.stringify(arr);
document.getElementById("demo").innerHTML = myJSON;
Exceptions
Parsing Data
JSON cannot store Date objects.
JSON.stringify() will convert all dates to strings.
Example
var obj = { "name":"tutorialpro", "initDate":new Date(), "site":"www.tutorialpro.org"};
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
You can then convert the string back to a Date object.
Parsing Functions
JSON does not allow functions. JSON.stringify() will remove functions from a JavaScript object, including both key and value.
Example
var obj = { "name":"tutorialpro", "alexa":function () {return 10000;}, "site":"www.tutorialpro.org"};
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
You can avoid this issue by converting the function to a string before executing the JSON.stringify() function:
Example
var obj = { "name":"tutorialpro", "alexa":function () {return 10000;}, "site":"www.tutorialpro.org"};
obj.alexa = obj.alexa.toString();
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
obj.alexa = obj.alexa.toString(); var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
It is not recommended to use functions in JSON.
Browser Support
The JSON.stringify() function is supported by all major browsers:
- Firefox 3.5
- Internet Explorer 8
- Chrome
- Opera 10
- Safari 4