JSON Flashcards

1
Q

Convert an object to a string

What gets lost? (5 things)

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does JSON stand for?

A

JavaScript Object Notation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is JSON?

A

is a general format to represent values and objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

IS JSON just for javascript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Convert a JSON string into an object

A

JSON.parse()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What happens to:
' 
`
and 
" 
when stringified?
A

all become “

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

JSON is
data-only language-independent specification
What does this mean?

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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);

A

// Error: Converting circular structure to JSON

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the replacer do?
JSON.stringify(value, replacer, space)
What are it’s limitations?
How do you fix it?

A
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}
}
*/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a replacer function

JSON.stringify(value, replacer, space)

A
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;
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What happens to dates that get stringified?

A
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)
  }
*/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does a toJSON() method on an object do?

A

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
  }
*/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
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]              
}`;
A
// 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens with this?

let user = {
  date: new Date(),
}

let apple = JSON.stringify(user);

let pear = JSON.parse(apple);

console.log(pear.date.getTime());

A

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;
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly