Objects and JSON Lesson

Scene

An object stores related information as key-value pairs. The key gives the property a name, and the value stores the data. Objects are useful when one thing has several details that belong together.

JSON is a text format based on objects and arrays. It is commonly used when data moves between a website and a server because it can be sent as text and then parsed back into JavaScript data.

Script

Code Runner Challenge

Objects and JSON Lesson

View IPYNB Source
%%js

// CODE_RUNNER: Objects and JSON Lesson

const player = {
  name: "Nova",
  level: 4,
  health: 75
};

const savedPlayer = JSON.stringify(player);
const loadedPlayer = JSON.parse(savedPlayer);

console.log(loadedPlayer.name + " is level " + loadedPlayer.level);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

The object groups the player data. JSON.stringify turns it into text, and JSON.parse turns the text back into usable JavaScript data.