Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JSON.stringify()

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:


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

=

[

"

Google

"

,

"

Runoob

"

,

"

Taobao

"

,

"

Facebook

"

]

;

var

myJSON

=

JSON

.

stringify

(

arr

)

;

myJSON is a string.

We can send myJSON to the server:

Example

var

arr

=

[

"

Google

"

,

"

Runoob

"

,

"

Taobao

"

,

"

Facebook

"

]

;

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: