JSON Flashcards
Convert an object to a string
What gets lost? (5 things)
JSON.stringify()
JSON-encoded or serialized or stringified or marshalled object
Function properties (methods). Symbolic keys and values. Properties that store undefined. Array properties Map, weakMap, Set, weakSet objects
What does JSON stand for?
JavaScript Object Notation
What is JSON?
is a general format to represent values and objects
IS JSON just for javascript?
Initially it was made for JavaScript, but many other languages have libraries to handle it as well
So it’s easy to use JSON for data exchange when the client uses JavaScript and the server is written on Ruby/PHP/Java/Whatever.
Convert a JSON string into an object
JSON.parse()
What happens to: ' ` and " when stringified?
all become “
JSON is
data-only language-independent specification
What does this mean?
some JavaScript-specific object properties are skipped by JSON.stringify.
Namely:
Function properties (methods). Symbolic keys and values. Properties that store undefined.
let user = { sayHi() { // ignored alert("Hello"); }, [Symbol("id")]: 123, // ignored something: undefined // ignored }; alert( JSON.stringify(user) ); // {} (empty object)
What happens with this code?
let room = {
number: 23
};
let meetup = { title: "Conference", participants: ["john", "ann"] };
meetup. place = room;
room. occupiedBy = meetup;
JSON.stringify(meetup);
// Error: Converting circular structure to JSON
What does the replacer do?
JSON.stringify(value, replacer, space)
What are it’s limitations?
How do you fix it?
Makes it so only those keys that match an element of the replacer array will be stringified (it can be a function too) Including any nested keys: let meetup = { title: "Conference", participants: [{name: "John"}, {name: "Alice"}], place: "Quebec" };
alert( JSON.stringify(meetup, ['title', 'participants']) ); // {"title":"Conference","participants":[{},{}]}
‘name’ and ‘name’ are skipped
Have to do this: let room = { number: 23 };
let meetup = { title: "Conference", participants: [{name: "John"}, {name: "Alice"}], place: room // meetup references room };
room.occupiedBy = meetup; // room references meetup
alert( JSON.stringify(meetup, ['title', 'participants', 'place', 'name', 'number']) ); /* { "title":"Conference", "participants":[{"name":"John"},{"name":"Alice"}], "place":{"number":23} } */
Write a replacer function
JSON.stringify(value, replacer, space)
let room = { number: 23 };
let meetup = { title: "Conference", participants: [{name: "John"}, {name: "Alice"}], place: room // meetup references room };
room.occupiedBy = meetup;
JSON.stringify(meetup, function replacer(key, value) {
return (key == ‘occupiedBy’) ? undefined : value;
})
What happens to dates that get stringified?
let meetup = { title: "Conference", date: new Date(Date.UTC(2017, 0, 1)), room };
alert( JSON.stringify(meetup) ); /* { "title":"Conference", "date":"2017-01-01T00:00:00.000Z", // (1) "room": {"number":23} // (2) } */
What does a toJSON() method on an object do?
Defines what is returned when JSON.stringify is called.
let room = { number: 23, toJSON() { return this.number; } };
let meetup = { title: "Conference", room };
alert( JSON.stringify(room) ); // 23
alert( JSON.stringify(meetup) ); /* { "title":"Conference", "room": 23 } */
What's wrong (or right) which each of the JSON properties? let json = { name: "John", "surname": 'Smith', 'isAdmin': false "birthday": new Date(2000, 2, 3), "friends": [0,1,2,3] }`;
// mistake: property name without quotes // mistake: single quotes in value (must be double) // mistake: single quotes in key (must be double) // mistake: no "new" is allowed, only bare values // here all fine
What happens with this?
let user = { date: new Date(), }
let apple = JSON.stringify(user);
let pear = JSON.parse(apple);
console.log(pear.date.getTime());
error.
Dates are converted to strings and they are not converted to date objects when parsed.
How do you fix this? (scroll down)
.
.
..
..
.
.
.
..
.
..
.
..
.
..
Use a reviver
JSON.parse(text, reviver)
let meetup = JSON.parse(str, function(key, value) { if (key == 'date') return new Date(value); return value; });