JSON (JavaScript Object Notation) is a lightweight data format used for data exchange between a server and a client.
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
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}'
Create a JavaScript object and convert it into a JSON string. Then, convert the JSON string back into an object.
JSON.parse()
do?JSON.stringify()
do?Answers: Converts JSON to an object; converts an object to a JSON string; data exchange between client and server.