Working with JSON in JavaScript


  JSON (JavaScript Object Notation) is a lightweight and easy-to-read data exchange format. JavaScript provides the `JSON.stringify` and `JSON.parse` methods for working with JSON.


Syntax:


  Let's see how to convert an object to JSON and vice versa:

const user = {
  nombre: "Juan",
  edad: 30
};

const userJSON = JSON.stringify(user); // Convert object to JSON
console.log(userJSON);

const usuarioObjeto = JSON.parse(userJSON); // Convert JSON to object
console.log(usuarioObjeto);

Purpose:


  The `JSON.stringify` method converts a JavaScript object into a JSON string, while `JSON.parse` converts a JSON string into a JavaScript object.


Exercises


The rest of the content is available only for registered and premium users!



What does the JSON.stringify() method do?



JavaScript Concepts and Reference

How is JSON used in JavaScript? How to manipulate JSON with JavaScript? How to write JSON with JavaScript? How to parse a JSON file in JavaScript? Iterate JSON JavaScript JSON objects in JavaScript Import JSON in JavaScript Extract data from JSON JavaScript Make a JSON in JavaScript JSON stringify Example JSON JSON parse