×

javascript · level 2 · unit 7 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

turns objects into text, and back again.

Programs send each other data as plain text. JSON is the most common way to write it. It looks almost exactly like JavaScript objects and arrays, except that every key and every string sits in double quotes.

turns an object into JSON text, ready to send or save. does the opposite: it reads JSON text and builds a real object, so you can use dots again.

Text in, object out. Badly written JSON makes JSON.parse crash with a SyntaxError.

const pet = { name: "Rex", age: 3 };
const text = JSON.stringify(pet);
console.log(text, typeof text);
const reply = '{"name":"Tibbles","age":7,"toys":["mouse","string"]}';
const cat = JSON.parse(reply);
console.log(cat.name, cat.toys[1]);

Single quotes make a string too, which is handy when the text is full of double quotes.