JSON.stringify()

The conventional use of JSON is for data exchange with web servers.

When sending data to a web server, the data must be a string.

Through JSON.stringify() Convert the JavaScript object to a string.

Stringify JavaScript Object

Imagine we have this object in JavaScript:

var obj = { name:"Bill Gates", age:62, city:"Seattle"};

Please use the JavaScript function JSON.stringify() Convert it to a string.

var myJSON = JSON.stringify(obj);

The result will be a string that conforms to JSON syntax.

myJSON is currently a string and ready to be sent to the server:

Example

var obj = { name:"Bill Gates", age:62, city:"Seattle"};
var myJSON =  JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;

Try It Yourself

You will learn in the next chapter how to send JSON to the server.

Stringify JavaScript Array

You can also stringify a JavaScript array:

Imagine we have this array in JavaScript:

var arr = [ "Bill Gates", "Steve Jobs", "Elon Musk" ];

Please use the JavaScript function JSON.stringify() Convert it to a string.

var myJSON = JSON.stringify(arr);

The result will be a string that conforms to JSON syntax.

myJSON is currently a string and ready to be sent to the server:

Example

var arr = [ "Bill Gates", "Steve Jobs", "Elon Musk" ];
var myJSON =  JSON.stringify(arr);
document.getElementById("demo").innerHTML = myJSON;

Try It Yourself

You will learn in the next chapter how to send JSON to the server.

Exception

Date stringification

In JSON, date objects are not allowed.JSON.stringify() The function will convert any date to a string.

Example

var obj =  { "name":"Bill Gates", "today":new Date(), "city":"Seattle"};
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;

Try It Yourself

You can convert a string back to a date object at the receiving end.

Function Stringification

Functions are not allowed as object values in JSON.

JSON.stringify() The function will remove any functions from the JavaScript object, including keys and values:

Example

var obj =  { "name":"Bill Gates", "age":function () {return 62;}, "city":"Seattle"};
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;

Try It Yourself

If you run JSON.stringify() The function has been converted to a string before the function, this step can be omitted.

Example

var obj =  { "name":"Bill Gates", "age":function () {return 62;}, "city":"Seattle"};
obj.age = obj.age.toString();
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;

Try It Yourself

You should avoid using functions in JSON, as functions will lose their scope and you will also need to use eval() Convert them back to functions.

Browser Support

All mainstream browsers and the latest ECMAScript (JavaScript) standards include JSON.stringify() Function:

The numbers in the following table specify full support JSON.stringify() First Browser Version of the Function:

Yes 8.0 3.5 4.0 10.0