Trusted answers to developer questions

What is JSON.stringify in JavaScript?

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

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

RELATED TAGS

json
stringify
parse
javascript
js
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?