JSON
JSON is a text format for data. JSON.stringify turns a value into a JSON string and JSON.parse turns the string back. Keys and strings must use double quotes in JSON.
Always wrap JSON.parse in try/catch when the text comes from outside, because bad text throws an error.
Example
const user = { name: "Ada", skills: ["html", "css"], active: true };
const text = JSON.stringify(user);
console.log(text);
const back = JSON.parse(text);
console.log(back.skills[1]);
try {
JSON.parse("{bad json}");
} catch (e) {
console.log("Invalid JSON");
}Exercise
Turn the object into JSON text.
JSON.(obj)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.