How Does the JSON.parse() Method Works in JavaScript? Last Updated : 21 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The JSON.parse() method in JavaScript is used to convert a JSON string into a JavaScript object. This method is essential for working with JSON data, especially when fetching or processing data from APIs or external sources.Converts a JSON-formatted string into a JavaScript object.Maintains the structure of arrays, nested objects, and data types like numbers and strings.Supports a reviver function to transform values during parsing.It throws an error if the input JSON string is invalid. JavaScript const jsonS = '{"name": "Aarav", "age": 22, "city": "Delhi"}'; const obj = JSON.parse(jsonS); console.log(obj.name); OutputAarav jsonString is a valid JSON string.JSON.parse() converts the string into a JavaScript object.Object properties are accessed using dot notation.How the JSON.parse() Method Works1. Parsing Simple JSON StringsThe method processes JSON strings and maps key-value pairs to a JavaScript object. JavaScript const jsonS = '{"product": "Mobile", "price": 12000}'; const obj = JSON.parse(jsonS); console.log(obj.product); OutputMobile 2. Handling JSON ArraysIt seamlessly parses JSON strings representing arrays into JavaScript arrays. JavaScript const jsonA = '[{"name": "Riya"}, {"name": "Karan"}]'; const a = JSON.parse(jsonA); console.log(a[0].name); OutputRiya 3. Parsing Nested JSONThe method processes deeply nested JSON objects. JavaScript const nJson = '{"user": {"name": "Simran", "address": {"city": "Pune", "pin": 411001}}}'; const obj = JSON.parse(nJson); console.log(obj.user.address.city); OutputPune 4. Validating JSON During ParsingInvalid JSON strings throw a SyntaxError. You can use a try...catch block to handle these cases. JavaScript const invalidJson = '{"name": "Ajay", "age": 30'; try { JSON.parse(invalidJson); } catch (e) { console.error("Invalid JSON:", e.message); } 5. Using a Reviver FunctionThe reviver parameter modifies or filters values during the parsing process. JavaScript const jsonS = '{"price": "1500", "discount": "5"}'; const obj = JSON.parse(jsonS, (key, value) => { if (key === "price" || key === "discount") return parseFloat(value); return value; }); console.log(obj.price - obj.discount); Output1495 Comment More infoAdvertise with us Next Article JavaScript JSON parse() Method A amanv09 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-QnA JavaScript-JSON Similar Reads How to Parse JSON in JavaScript ? Parse JSON in JavaScript, accepting a JSON string as input and returning a corresponding JavaScript object with two methods, using JSON.parse() for parsing JSON strings directly and employing the fetch API to parse JSON responses from web APIs. These techniques are crucial for seamless data manipula 2 min read How to Parse JSON Data in JavaScript? To parse JSON data in JavaScript, you can use the JSON.parse() method. This method converts a JSON string into a JavaScript object, making it easier to work with the data.1. Parse Simple JSON StringsJavaScriptconst jsonS = '{"name": "Rahul", "age": 25, "city": "Mumbai"}'; const obj = JSON.parse(json 2 min read How does memory stacks work in Javascript ? Introduction: The memory stack is a mechanism in JavaScript that allows us to allocate and free memory. If a programmer wants to store some data, they must first create an empty part of the heap that is called a "stack." Then, they can push and then pop values on the stack. When working with strings 3 min read How to Master JSON in JavaScript? JSON is a text format for representing structured data, typically in the form of key-value pairs. It primarily sends data between a server and a client, especially in web APIs.Objects are enclosed in curly braces {} and contain key-value pairs.Arrays are enclosed in square brackets [] and hold value 5 min read JavaScript JSON parse() Method The JSON.parse() method is used to convert a JSON string into a JavaScript object. Itâs become important when dealing with data in JSON format, interacting with APIs, or storing data in the browser.It converts a JSON string into a JavaScript object.Throws a SyntaxError if the input string is not val 3 min read How to Catch JSON Parse Error in JavaScript ? JSON (JavaScript Object Notation) is a popular data interchange format used extensively in web development for transmitting data between a server and a client. When working with JSON data in JavaScript, it's common to parse JSON strings into JavaScript objects using the JSON.parse() method. However, 1 min read Like