Json Stringify
JSON is typically used to exchange data with a server.
When sending data to a server, the data is usually 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 (usually an object or an 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 the key and value of each member. It uses the return value instead of the original value. If this function returns `undefined`, the member is excluded. The key of the root object is an empty string: `""`.
If `replacer` is an array, only members with keys present in that array are converted. The conversion order of members is the same as the order of keys in the array. When the `value` parameter is also an array, the `replacer` array is ignored.
* **space:**
Optional. Adds indentation, spaces, and line breaks to the text. If `space` is a number, the returned text will be indented by that many spaces per level. If `space` is greater than 10, the text is indented with 10 spaces. `space` can also be a non-numeric value, such as `t`.
* * *
## JavaScript Object Conversion
For example, we send the following data to a server:
```javascript
var obj = {"name":"tutorial", "alexa":10000, "site":"www."};
We use the `JSON.stringify()` method to process the above data and convert it into a string:
```javascript
var myJSON = JSON.stringify(obj);
`myJSON` is now a string.
We can send `myJSON` to the server:
## Example
```javascript
var obj = {"name":"tutorial", "alexa":10000, "site":"www."};
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
[Try it Β»](#)
* * *
## JavaScript Array Conversion
We can also convert a JavaScript array into a JSON string:
## Example
```javascript
var arr = ["Google", "Tutorial", "Taobao", "Facebook"];
var myJSON = JSON.stringify(arr);
`myJSON` is now a string.
We can send `myJSON` to the server:
## Example
```javascript
var arr = ["Google", "Tutorial", "Taobao", "Facebook"];
var myJSON = JSON.stringify(arr);
document.getElementById("demo").innerHTML = myJSON;
[Try it Β»](#)
* * *
## Exceptions
### Parsing Data
JSON cannot store Date objects.
`JSON.stringify()` will convert all dates into strings.
## Example
```javascript
var obj = {"name":"Tutorial", "initDate":new Date(), "site":"www."};
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
[Try it Β»](#)
You can then convert the string back into a Date object later.
* * *
## Parsing Functions
JSON does not allow functions. `JSON.stringify()` will remove functions from a JavaScript object, including both the key and the value.
## Example
```javascript
var obj = {"name":"Tutorial", "alexa":function(){return 10000;}, "site":"www."};
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
[Try it Β»](#)
We can avoid this issue by converting the function to a string before executing `JSON.stringify()`:
## Example
```javascript
var obj = {"name":"Tutorial", "alexa":function(){return 10000;}, "site":"www."};
obj.alexa = obj.alexa.toString();
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
[Try it Β»](#)
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
YouTip