What is JSON.parse in JavaScript?

JavaScript Object Notation (JSON) is a light-weight data storage format for storing and transmitting data, 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 does the JSON.parse method do?

Most of the times, the data given is not in JSON format and must be converted into JSON before you can use it. That’s where the JSON.parse method comes into play. It converts a JSON string to a JSON object, which can then be used, changed and stored to a JSON file.


Example

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

var data = '{"platform": "Educative", "established": 2015}';
json = JSON.parse(data);
console.log(json.platform); // expected output: Educative
console.log(json.established); // expected output: 2015

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved