JavaScript Q&A Logo
JavaScript Q&A Part of the Q&A Network

What is JSON and how do I parse it in JavaScript?

Asked on Aug 24, 2024

Answer

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In JavaScript, you can parse JSON using the `JSON.parse()` method.
<!-- BEGIN COPY / PASTE -->
        const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
        const jsonObject = JSON.parse(jsonString);

        console.log(jsonObject.name); // Output: John
        <!-- END COPY / PASTE -->
Additional Comment:
  • JSON is often used to transmit data between a server and a web application.
  • The `JSON.parse()` method converts a JSON string into a JavaScript object.
  • Ensure the JSON string is properly formatted; otherwise, `JSON.parse()` will throw an error.
✅ Answered with JavaScript best practices.
← Back to All Questions