JSON
The syntax of JSON, JSON.parse and JSON.stringify, the difference from a JavaScript object and the access path to a nested value.
- 24 cards
- 3 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
A few cards from the deck
JSON: what is it?
Show the answer
A text format for exchanging data, derived from JavaScript's object notation, made of objects, arrays, strings, numbers, booleans and null.
It is readable by every language, not just JavaScript: that is why web APIs use it. It knows nothing of dates, functions or
undefined.What string does this program produce?
```javascript
JSON.stringify({ a: 1, b: undefined, c: [1, undefined], d: () => 1 })`{"a":1,"c":[1,null]}{"a":1,"b":null,"c":[1,null]}{"a":1,"c":[1]}{"a":1,"c":[1,null],"d":null}
Show the answer
{"a":1,"c":[1,null]}In an object,
undefinedor function properties are omitted (bandddisappear). In an array, they keep their place and becomenull.A valid JSON document is written with … quotes; it accepts neither a … nor a comment.
Show the answer
A valid JSON document is written with double quotes; it accepts neither a trailing comma nor a comment.
Single quotes and unquoted names work in a JavaScript literal, not in JSON. In an object, a trailing comma produces
Expected double-quoted property name.JSON's strict syntax: what is it?
Show the answer
JSON's rules: property names and strings are written in double quotes, with no comma after the last element and no comment.
{ a: 1 },{'a': 1}and{"a": 1,}are valid JavaScript literals, butJSON.parserejects them: for the third, it raisesExpected double-quoted property name in JSON.What does
JSON.stringify({ n: NaN })return?{"n":NaN}{"n":null}{"n":"NaN"}{}
Show the answer
{"n":null}JSON has no representation for
NaNor infinity: they becomenull. It is not an omission, unlikeundefinedin an object.