Open In App

How does the JSON.parse() method works in JavaScript ?

Last Updated : 30 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The JSON.parse() method in JavaScript is used to parse a JSON (JavaScript Object Notation) string and convert it into a JavaScript object. JSON is a lightweight data-interchange format that is easy for humans to read and write, and it is also easy for machines to parse and generate.

Example: Here, JSON.parse() take a JSON-formatted string (jsonString) and converts it into a JavaScript object (parsedObject). Once parsed, you can access the properties of the object as you would with any other JavaScript object.

Javascript




// A JSON string representing an object
const jsonString =
    '{"name": "John", "age": 30, "city": "New York"}';
 
// Using JSON.parse() to convert the JSON
// string into a JavaScript object
const parsedObject = JSON.parse(jsonString);
 
// Now parsedObject is a JavaScript
// object that you can work with
console.log(parsedObject.name); // Output: John
console.log(parsedObject.age);  // Output: 30
console.log(parsedObject.city); // Output: New York


Output

John
30
New York

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads