JSON Parsing Lesson

JSON parsing converts JSON text into JavaScript data. JSON text is just a string until the program parses it. After parsing, the program can access properties and loop through arrays normally.

Code Runner Challenge

JSON Parsing Lesson

View IPYNB Source
%%js

// CODE_RUNNER: JSON Parsing Lesson

const jsonText = '{"name":"Nova","level":4,"items":["key","map"]}';
const data = JSON.parse(jsonText);

console.log(data.name);
console.log(data.items[0]);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...
JSON is strict about formatting. Property names need double quotes, strings need double quotes, and extra commas are not allowed. Invalid JSON will cause JSON.parse to throw an error. The string becomes an object after JSON.parse runs. The code can then read the name property and the first item in the items array.