Tsallaka zuwa abun ciki
Justice Tech School

JSON in Practice

JSON.stringify turns values into text and JSON.parse reads it. Use a replacer, indentation and try catch because parsing can throw. Dates become strings.

Example
const data = { name: "Ada", skills: ["js"], when: new Date(0) };
const text = JSON.stringify(data, null, 2);
console.log(text);
try { JSON.parse("{bad"); } catch (e) { console.log(e.name); }
console.log(JSON.parse("[1,2]"));

Exercise

Pretty print.

JSON.stringify(x, null, )

Quiz Test

What can JSON.parse throw?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.