How does the JSON.parse() method works in JavaScript ?
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
Contact Us