How to Parse JSON Data in JavaScript? Last Updated : 18 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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 Strings JavaScript //Driver Code Starts const jsonS = '{"name": "Rahul", "age": 25, "city": "Mumbai"}'; //Driver Code Ends const obj = JSON.parse(jsonS); //Driver Code Starts console.log(obj.name); //Driver Code Ends OutputRahul The string is in JSON format.JSON.parse() converts it into a JavaScript object.You can access the object properties using dot notation or brackets.2. Parse JSON Array Strings JavaScript //Driver Code Starts const jsonA = '[{"name": "Anjali"}, {"name": "Vikas"}]'; //Driver Code Ends const a = JSON.parse(jsonA); a.forEach(person => console.log(person.name)); OutputAnjali Vikas The JSON string represents an array of objects.After parsing, you can iterate through the array and access each object.3. Parse Nested JSON JavaScript //Driver Code Starts const nested = '{"person": {"name": "Ravi", "address": {"city": "Delhi", "pin": 110001}}}'; //Driver Code Ends const obj = JSON.parse(nested); console.log(obj.person.address.city); OutputDelhi JSON.parse() handles nested objects seamlessly.You can access nested properties using chained dot notation.4. Parse JSON with Validation JavaScript //Driver Code Starts const jsonS = '{"name": "Pooja", "age": 28}'; //Driver Code Ends try { const obj = JSON.parse(jsonS); console.log(obj); } catch (e) { console.error("Invalid JSON:", e.message); } Output{ name: 'Pooja', age: 28 } Wrapping JSON.parse() in a try...catch block handles invalid JSON inputs.This is especially useful when dealing with external or unverified data.5. Parse JSON with a Reviver Function JavaScript //Driver Code Starts const jsonS = '{"name": "Amit", "age": "30"}'; //Driver Code Ends const obj = JSON.parse(jsonS, (key, value) => { if (key === "age") return parseInt(value); return value; }); //Driver Code Starts console.log(obj.age); //Driver Code Ends Output30 A reviver function lets you transform values during parsing.Here, the age is converted from a string to a number. Comment More infoAdvertise with us Next Article How to Parse JSON Data in JavaScript? J jaykush Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions 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 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 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 How to Create JSON String in JavaScript? JSON strings are widely used for data interchange between a server and a client, or between different parts of a software system. So converting objects to JSON strings is very important for good client-server communication. Below are the following approaches to creating a JSON string: Table of Conte 2 min read How to Serialize JSON in JavaScript ? JSON (JavaScript Object Notation) serialization is a fundamental concept in JavaScript, allowing the conversion of JavaScript objects into strings that can be easily transmitted over a network or stored in a file. We will explore how to serialize JSON in JavaScript using JSON.stringify(). Approach I 1 min read How to Parse XML in JavaScript? Parsing XML data is important because it allows JavaScript applications to extract structured information from XML documents. We will explore two different approaches to Parse XML in JavaScript. Below are the approaches to parsing XML in JavaScript: Table of Content Using DOM ParserUsing xml2js Libr 2 min read How Does the JSON.parse() Method Works in JavaScript? 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 stru 2 min read How to Convert Blob Data to JSON in JavaScript ? When dealing with Blob data in JavaScript, such as binary data or files, we may need to convert it into JSON format for doing so JavaScript provides us with various methods as listed below. Table of Content Using FileReader APIUsing TextDecoder APIUsing FileReader APIIn this approach, we first use t 2 min read How to Convert Map to JSON in JavaScript ? In JavaScript, when working with data, you might encounter situations where you need to convert a Map object into a JSON format. This can be useful for sending data over the network, storing data in local storage, or interfacing with APIs that expect JSON data. Converting a Map to JSON means convert 3 min read How to Convert XML to JSON in JavaScript? To convert XML to JSON in JavaScript, various methods and libraries and be used. Here, we use xml-js library that provides xml2json function to convert XML to JSON data. It takes XML data as input and gives the JSON objects as output. We can also use the DOMParser from the xmldom package to convert 2 min read Like