JSON.stringify()
JSON is usually used to exchange data with the server.
When sending data to the server, it is generally 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 convert (usually an object or array).
- replacer:
Optional. A function or array used to transform the result.
If replacer is a function, JSON.stringify will call that function with the key and value of each member and use the return value instead of the original value. If the function returns undefined, the member is excluded. The key of the root object is an empty string: "".
If replacer is an array, only members whose keys are 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 is indented by that many spaces at each level; if space is greater than 10, the text is indented by 10 spaces. space can also be a non-number, such as a tab character.
JavaScript object conversion
For example, we send the following data to the server:
var
obj
=
{
"
name
"
:
"
runoob
"
,
"
alexa
"
:
10000
,
"
site
"
:
"
www.runoob.com
"
}
;
We use the JSON.stringify() method to process the above data and convert it into a string:
var
myJSON
=
JSON
.
stringify
(
obj
)
;
myJSON is a string.
We can send myJSON to the server:
Example
var
obj
=
{
"
name
"
:
"
runoob
"
,
"
alexa
"
:
10000
,
"
site
"
:
"
www.runoob.com
"
}
;
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
var
arr
=
[
"
"
,
"
Runoob
"
,
"
Taobao
"
,
"
"
]
;
var
myJSON
=
JSON
.
stringify
(
arr
)
;
myJSON is a string.
We can send myJSON to the server:
Example
var
arr
=
[
"
"
,
"
Runoob
"
,
"
Taobao
"
,
"
"
]
;
var
myJSON
=
JSON
.
stringify
(
arr
)
;
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
myJSON
;
Try it »
exception
Parsing data
JSON cannot store a Date object.
JSON.stringify() converts all dates into strings.
Example
var
obj
=
{
"
name
"
:
"
Runoob
"
,
"
initDate
"
:
new
Date
(
)
,
"
site
"
:
"
www.runoob.com
"
}
;
var
myJSON
=
JSON
.
stringify
(
obj
)
;
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
myJSON
;
Try it »
You can then convert the string back into a Date object later.
The reviver function
JSON does not allow functions; JSON.stringify() removes the functions of a JavaScript object, including the key and value.
Example
var
obj
=
{
"
name
"
:
"
Runoob
"
,
"
alexa
"
:
function
(
)
{
return
10000
;
}
,
"
site
"
:
"
www.runoob.com
"
}
;
var
myJSON
=
JSON
.
stringify
(
obj
)
;
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
myJSON
;
Try it »
We can convert the function into a string before executing JSON.stringify() to avoid the above problem:
Example
var
obj
=
{
"
name
"
:
"
Runoob
"
,
"
alexa
"
:
function
(
)
{
return
10000
;
}
,
"
site
"
:
"
www.runoob.com
"
}
;
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
Mainstream browsers all support the JSON.stringify() function:
- Firefox 3.5
- Internet Explorer 8
- Chrome
- Opera 10
- Safari 4