The role of JSON.parse is to parse a JSON string into a JavaScript object. In other words, it converts a JSON data into a format that can be understood by JavaScript.
For example, let's assume that we have the following JSON string:
'{"name":"John", "age":30, "city":"New York"}'
To convert this string into a JavaScript object, we can use the JSON.parse() method, like this:
const person = JSON.parse('{"name":"John", "age":30, "city":"New York"}');
console.log(person.name); // "John"
console.log(person.age); // 30
console.log(person.city); // "New York"
In the code above, the JSON.parse() method parses the string into a JavaScript object and assigns it to the person variable. Then we access the object's properties to print their values.
The JSON.parse() method is the opposite of the JSON.stringify() method, which converts a JavaScript object into a JSON string.
Top comments (0)