w1coding.comAll tutorials

JavaScript JSON

JavaScript lessons
  1. Environment Setup
  2. Introduction
  3. Enviroment
  4. Basics
  5. Variables
  6. Operators
  7. Condition
  8. Functions
  9. Arrays
  10. Bjects
  11. DOM
  12. Objects
  13. Colors
  14. DOM
  15. Events
  16. Validation
  17. JSON
  18. Error
  19. Callbacks
  20. Promises
  21. Fetch
  22. Local
  23. Modules
  24. Animation
  25. Classes
  26. Iframes
  27. Responsive

JSON in JavaScript

JSON (JavaScript Object Notation) is a lightweight data format used for data exchange between a server and a client.

Parsing JSON

Use JSON.parse() to convert a JSON string into an object:

      
        let jsonString = '{"name": "Alice", "age": 25}';
        let jsonObj = JSON.parse(jsonString);
        console.log(jsonObj.name); // Outputs: Alice
      
    

Stringifying JSON

Use JSON.stringify() to convert an object into a JSON string:

      
        let person = { name: "Alice", age: 25 };
        let jsonString = JSON.stringify(person);
        console.log(jsonString); // Outputs: '{"name":"Alice","age":25}'
      
    

Activity

Try It Yourself!

Create a JavaScript object and convert it into a JSON string. Then, convert the JSON string back into an object.

Quiz

Quick Quiz

  1. What does JSON.parse() do?
  2. What does JSON.stringify() do?
  3. What is JSON used for?

Answers: Converts JSON to an object; converts an object to a JSON string; data exchange between client and server.