What is JSON.stringify in JavaScript?

JavaScript Object Notation (JSON) is a light-weight data storage format for storing and transmitting data. It is stored in the form of key-value pairs.

What sets JSON apart from other data formats is its human-readable and self-describing properties.

Data in JSON format
Data in JSON format

What is JSON.stringify?

As compared to JSON, which is a data storage and transmission notation, JSON.stringify is a method in JavaScript which, given a JSON object as an argument, returns JSON text in string format.


What does the JSON.stringify method do?

As described above, a big reason for converting data to JSON format is so that it can be transmitted. But to send and receive data to a web server, the data has to be in string format. That’s where the JSON.stringify method comes into play. It converts a JSON object to JSON text stored in a string, which can then be transmitted to the web server.


Example

Let’s see how we can use the JSON.stringify method to convert a string, data, into a JSON object.

var json = {"platform": "Educative",
"established": 2015};
data = JSON.stringify(json);
console.log(typeof data); // expected output: string
console.log(data); // expected output: json data in string format

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved