Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to Master JSON in JavaScript?
Next article icon

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 } 

Output
Rahul 
  • 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));   

Output
Anjali 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);  

Output
Delhi 
  • 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 } 

Output
30 
  • A reviver function lets you transform values during parsing.
  • Here, the age is converted from a string to a number.

Next Article
How to Master JSON in JavaScript?

J

jaykush
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 valu
    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 str
    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 Add Backslash in JSON String JavaScript ?
    In JavaScript, adding a backslash to a JSON string is important to properly escape special characters, ensuring the integrity and correctness of the JSON format for data processing and storage. Table of Content Using JSON.parse() and JSON.stringify()Using for LoopUsing Array.prototype.map() and Stri
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences