Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Quiz: Object to Text
What does this code print?
console.log(JSON.stringify({ a: 1 }));{"a":1}{ a: 1 }[object Object]a,1Hint : JSON wraps keys in double quotes and drops the spaces.
Exercise 2 β Quiz: Result Type
What does this code print?
console.log(typeof JSON.stringify([1, 2]));"string""object""array""json"Hint : The method's whole job is to produce text.
Exercise 3 β Quiz: Array to Text
What does this code print?
console.log(JSON.stringify(["Zap", 4]));["Zap",4]Zap,4[Zap, 4]2Hint : Arrays keep their brackets, strings keep their quotes.
Exercise 4 β Quiz: The Dropped Method
What does this code print?
const o = { a: 1, f() { return 2; } };
console.log(JSON.stringify(o));{"a":1}{"a":1,"f":2}{"a":1,"f":null}Hint : JSON stores data, never behaviour.
Exercise 5 β Quiz: Make It Readable
Print the object indented by 2 spaces.
Hint : The middle slot stays empty, the last one is the indentation.
Exercise 6 β Quiz: Text to Object
What does this code print?
const o = JSON.parse('{"a":5}');
console.log(o.a);5"a"undefinedHint : After parsing, the keys are real properties again.
Exercise 7 β Quiz: Parsed Type
What does this code print?
console.log(typeof JSON.parse('{"a":1}'));"object""string""json""number"Hint : The method rebuilds a real value from the text.
Exercise 8 β Quiz: Reading a Number
What does this code print?
const o = JSON.parse('{"level":4}');
console.log(o.level + 1);541undefinedHint : Numbers come back as real numbers, not text.
Exercise 9 β Quiz: Load It
Rebuild the object and print its name.
Hint : One method reads text into an object, which then has its keys back.
Exercise 10 β Quiz: Invalid JSON
What happens when `JSON.parse("{a: 1}")` runs?
Hint : JSON is strict about quotes around keys.
Exercise 11 β Quiz: Round Trip Identity
What does this code print?
const a = { x: 1 };
const b = JSON.parse(JSON.stringify(a));
console.log(a === b);falsetrueundefinedHint : Parsing builds a brand-new object.
Exercise 12 β Quiz: Deep Copy
What does this code print?
const a = { s: { hp: 40 } };
const b = JSON.parse(JSON.stringify(a));
b.s.hp = 5;
console.log(a.s.hp);405undefinedHint : The round trip rebuilds every level, not just the first.
Exercise 13 β Quiz: Parse Safely
Protect the parse from corrupted text.
Hint : Outside text can always be invalid, so the read goes in the protected area.
Exercise 14 β Quiz: Before Reading
A server answer sits in `response` as text. What is the first step?
Hint : Text has no properties until it is rebuilt.
Exercise 15 β Quiz: Nested Path
What does this code print?
const d = JSON.parse('{"team":[{"name":"Zap"}]}');
console.log(d.team[0].name);Zapundefined"team"Hint : Array, then first object, then property.
Exercise 16 β Quiz: Missing Field
What does this code print?
const d = JSON.parse('{"status":"ok"}');
console.log(d.trainer?.badges ?? 0);0undefinednullHint : One symbol survives the missing link, the other supplies the fallback.
Exercise 17 β Quiz: Crashing Dive
What happens on the last line?
const d = JSON.parse('{"status":"error"}');
console.log(d.team.length);0undefinedHint : Reading a property of undefined is not allowed.
Exercise 18 β Quiz: Safe Count
Print the team size, or 0 when there is no team.
Hint : Survive the missing link, then replace the empty answer.
Exercise 19 β Quiz: Which Layer?
The JSON is valid but `status` is `"error"`. What handles it?
Hint : Parsing succeeded; the problem is what the answer says.
Exercise 20 β Quiz: Which Layer, Again
The text that arrived is `{broken`. What handles it?
Hint : This input never becomes an object at all.
Exercise 21 β Quiz: Key Quotes
In JSON text, how are keys written?
Hint : JSON has one rule for keys and it never bends.
Exercise 22 β Quiz: Set in JSON
What does this code print?
console.log(JSON.stringify(new Set([1, 2])));{}[1,2]Set(2)Hint : JSON doesn't know this collection; convert it to an array first.
Exercise 23 β Quiz: undefined Property
What does this code print?
console.log(JSON.stringify({ a: 1, b: undefined }));{"a":1}{"a":1,"b":undefined}{"a":1,"b":null}Hint : A value that means nothing is simply left out.
Exercise 24 β Quiz: Why JSON?
Why turn an object into JSON before saving or sending it?
Hint : Think about what can actually travel outside the program.
Exercise 25 β Quiz: The Pair
Which method is the opposite of `JSON.stringify()`?
JSON.parse()JSON.read()JSON.load()JSON.object()Hint : One turns objects into text, the other turns text back into objects.