JavaScript facilitates the capability to store and transport the data while sending the data from a server to a web page with the help of JSON. JSON (JavaScript Object Notation) is a language-independent lightweight data interchange format, easy to read and write. JSON provides different methods that help to convert the JS object to a string or provides parsing of a JSON string, which is written in a JSON format to a JavaScript object. For this, JSON.parse() & JSON.stringify methods are used. In this article, we will see JSON.parse() method & JSON.stringify method, their implementation & difference between them.
JSON.stringify() Method is used to convert JavaScript objects into JSON strings. This method only takes a single argument, and that argument is an object to be converted into JSON strings. It works oppositely to JSON.parse() method. JSON.stringify can take replacer parameters as a function, we can write logic on key-value pairs of objects. While receiving data from a web server (like a node server), data has to be in the string format, & after parsing data with JSON.parse(), the data become an Object. The format of the Date is not allowed in JSON, So you can include the Date in the string.
Example: This example describes the basic usage of JSON.stringify() Method.
Javascript
const myInfo = { Name: "GFG" , Age:22, Department : "Computer Science and Engineering" , Year: "3rd" } const Obj = JSON.stringify(myInfo); console.log(Obj) |
Explanation: Here, the variable myInfo contains a JavaScript Object with 4 Properties: Name, Age, Department, and Year. The JSON.stringify() method returns the JSON string, which is stored inside the object Obj.
Output:
{"Name":"GFG","Age":22,"Department":"Computer Science and Engineering","Year":"3rd"}
JSON.parse() Method is used to convert a JSON string into a JavaScript Object. JavaScript objects are basically storage that store some data of that respective class. It takes a single argument, which is the JSON string to be parsed. Note that the string to be parsed must be in backticks ‘ ’ and all the words must be in double cots. When sending data to a web server, The data has to be in string format. It can be used to store the data in the local storage of the browser because the browser store data in local storage in key-value pairs.
Example: This example describes the basic usage of JSON.parse() Method.
Javascript
const myInfo = `{ "Name" : "GFG" , "Age" :22, "Department" : "Computer Science and Engineering" , "Year" : "3rd" }` const Obj = JSON.parse(myInfo); console.log(Obj.Name) console.log(Obj.Age) |
Explanation: Here, the myInfo variable stored a JSON string in form of an object with the two properties Name, age, Department, and Year. We have used JSON.parse() to parse the string and converted it into a whole new object. The variable obj stored that parsed object and we can access the properties of those objects using the dot operator.
Output:
GFG 22
Difference Between JSON.stringify() Method & JSON.parse() Method:
JSON.stringify() Method |
JSON.parse() Method |
---|---|
Convert data JavaScript object into JSON string. |
Convert a JSON string into a JavaScript Object. |
The string must be wrapped in double cots. example: “String”. |
No need to wrap the string in double cots. |
while sending data to a web server, data has to be in string format. |
While receiving data from the web server data has to be in the string. |
The key must be a string with double quotes. |
Keys can be anything such as String, Number, or Character, but the object must be wrapped in quotes i.e ‘{key:, value}’. |
It allow 2 extra (optional) parameters, i.e value, replacer, and space, for eg., JSON.stringify(value, replacer, space). |
It allow one extra (optional) parameter, i.e reviewer, for eg, JSON.parse(value, reviewer). |