JSON
JavaScript · Unit 14: Modern classes
- Full plan
- JavaScript
- 10 exercises
To store an object or send it to another program you have to turn it into text. JSON.stringify() builds that text: property names go in double quotes and no space is left over.
const car = {
brand: "Nissan",
year: 2020,
};
const text = JSON.stringify(car);
console.log(text);
console.log(typeof text);Prints
{"brand":"Nissan","year":2020}
stringThe rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Complete the code
Fill in the blank to turn the cart into text.
3. Predict the output
What does this code print?
4. Complete the code
The data arrived as text. Fill in the blank so you can use it as an object.
5. Find the bug
This program should print 8, twice the height of the box. Which line has the error?
6. Predict the output
What does this code print?
7. Multiple choice
An object is converted with
JSON.stringifyand comes back withJSON.parse. What survives the trip?8. Put the lines in order
Put in order the program that reads a chip saved as text, builds it again with its class and uses its method.
9. Find the case that fails
points(text)reads a JSON text and returns the value ofpoints, or 0 if the text isn't valid JSON. Which call does it fail on?10. Predict the output
Two different chips with the same number. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.