JSON Flashcards

1
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
2
Q

List the properties that make up the following JSON string:
~~~
‘{“name”:”John”, “age”:30, “car”:null}’
~~~

A
  • name
  • age
  • car

Note that each property has a corresponding value

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

How would you access the data from the following JSON string as an object?
~~~
‘{“name”:”John”, “age”:30, “car”:null}’
~~~

A

Ex:
~~~
let personName = obj.name;
let personAge = obj.age;
~~~

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

List some reasons why you would use JSON:

A
  • Format of JSON is syntactically similar to code for creating Javascript objects, so Javascript programs can easily convert JSON data into Javascript objects
  • JSON format is text only (utilizes TUI’s, or text based user interfaces, meaning no graphics are used), so JSON can easily be sent between computers and used by any programming language
  • Hence, JSON makes it possible to store Javascript objects as text
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the built in Javascript function for converting JSON strings into Javascript objects?

A

JSON.parse()

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

What is the built in function for converting and object into a JSON string?

A

JSON.stringify()

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

JSON syntax is a _____ of Javascript syntax.

A

subset

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

What are basic syntax rules in JSON? (4 things)

A
  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

JSON data is written as ______/_______ pairs.

A

name/value pairs, or key/value pairs

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

What do name/value pairs consist of in JSON?

A

A field name (in double quotes), followed by a colon, followed by a value:
~~~
“name”:”John”
~~~

(note JSON names and keys require double quotes)

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

List the data types that can be used as values in JSON:

A
  • a string
  • a number
  • an object
  • an array
  • a boolean
  • null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

True or False:
String values must be written with double quotes in JSON.

A

True

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

Write a sample program in Javascript for an object that contains the following information:
* Properties: name, age, city
* Values: “John”, 31, “New York”

A
person = {name:"John", age:31, city:"New York"};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How would you access the name for this object?
~~~
person = {name:”John”, age:31, city:”New York”};
~~~

A
//returns John
person.name;
//alternative way to access it
person["name"]; 
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How would you modify the name in this object to Gilbert?
~~~
person = {name:”John”, age:31, city:”New York”};
~~~

A
person.name = "Gilbert";
//or
person["name"] = "Gilbert";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How is JSON similar to XML? (4 ways)

A
  • Both JSON and XML are “self describing” (human readable)
  • Both JSON and XML are hierarchical (values within values)
  • Both JSON and XML can be parsed and used by lots of programming languages
  • Both JSON and XML can be fetched with an XMLHttpRequest
17
Q

How is JSON different than XML? (4 ways)

A
  • JSON doesn’t use end tag
  • JSON is shorter
  • JSON is quicker to read and write
  • JSON can use arrays
18
Q

Why would you use JSON over XML?

A
  • XML is much more difficult to parse than JSON.
  • JSON is parsed into a ready-to-use JavaScript object.
19
Q

Nam 3 INVALID data types that JSON does not accept:

A
  • a function
  • a date
  • undefined
20
Q

How would you write an object in JSON with the following properties:
* Object name: “employee”
* object properties: name: John, age: 30, city: New York

A
{
"employee":{"name":"John", "age":30, "city":"New York"}
}
21
Q

Are the following JSON examples valid or not:
1.

{
"employees":["John", "Anna", "Peter"]
}

2.
~~~
{“sale”:true}
~~~
3.
~~~
{“middlename”:null}
~~~
A
  1. Valid
  2. Valid
  3. Valid
22
Q

How is JSON used with web servers?

A

Used to exchange data to/from web servers.

(data received is always in string form)

23
Q

At what point does the JSON data become a Javascript object?

A

Once the data is parsed with JSON.parse()

24
Q

How would you parse the following JSON data and store it into an object called ‘obj’?
~~~
‘{“name”:”John”, “age”:30, “city”:”New York”}’
~~~

A
const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}');

(ensure the text you are parsing is in JSON format, or else it’ll error)

25
Q

What happens when using JSON.parse() on a JSON derived from an array?

A

Method will return a Javascript array, instead of a Javascript object.
Ex:
~~~
const text = ‘[“Ford”, “BMW”, “Audi”, “Fiat”]’;
const myArr = JSON.parse(text);
~~~

26
Q

How would you parse out a date object from JSON to Javascript?

A

Must be included as a string in JSON, as date objects are not allowed in JSON

27
Q

How would you parse out a function in JSON?

A

Must write the function as a string in JSON.
Ex:
~~~
const text = ‘{“name”:”John”, “age”:”function () {return 30;}”, “city”:”New York”}’;
const obj = JSON.parse(text);
obj.age = eval(“(“ + obj.age + “)”);

document.getElementById(“demo”).innerHTML = obj.name + “, “ + obj.age();
~~~

28
Q

When sending data to a web server with JSON, the data has to be a _______.

A

String

29
Q

How do you convert a Javascript object into a string?

A

JSON.stringify()
Ex:
~~~
const obj = {name: “John”, age: 30, city: “New York”};
const myJSON = JSON.stringify(obj);
//myJSON is now a string, and ready to be sent to a server:

~~~

(this can be done with arrays as well the same way)

30
Q

What does the following code do?
~~~
const myObj = {name: “John”, age: 31, city: “New York”};
const myJSON = JSON.stringify(myObj);
localStorage.setItem(“testJSON”, myJSON);

let text = localStorage.getItem(“testJSON”);
let obj = JSON.parse(text);
document.getElementById(“demo”).innerHTML = obj.name;
~~~

A

Stores the myObj in JSON and then retrieves the data.

31
Q

What is a JSON object literal?

A

Objects in JSON surrounded by curly braces.
* Contains key/value pairs separated by a colon
* Keys are strings and values are valid JSON data type

32
Q

How would you create a Javascript object from the following JSON object literal?
~~~
{“name”:”John”, “age”:30, “car”:null}

~~~

A
myObj = {"name":"John", "age":30, "car":null};

Can also create the object by parsing a JSON string:
~~~
myJSON = ‘{“name”:”John”, “age”:30, “car”:null}’;
myObj = JSON.parse(myJSON);
~~~

(simply set an object equal to an object literal, this is legal)

33
Q

How can you loop through object properties? How would you acces the property values?

A

Use a for-in loop. Use bracket notation to access the property values.
Ex 1: accessing property names
~~~
const myJSON = ‘{“name”:”John”, “age”:30, “car”:null}’;
const myObj = JSON.parse(myJSON);

let text = “”;
for (const x in myObj) {
text += x + “, “;
}
~~~
Example 2: accessing property values
~~~
const myJSON = ‘{“name”:”John”, “age”:30, “car”:null}’;
const myObj = JSON.parse(myJSON);

let text = “”;
for (const x in myObj) {
text += myObj[x] + “, “;
}
~~~

34
Q

Describe what this line of code is:
~~~
‘[“Ford”, “BMW”, “Fiat”]’
~~~

A
  • JSON string ‘ ‘ with a JSON array literal inside [ ]

(valid JSON array values are string, number, array, booleon, or null)

35
Q

How would you create a Javascript array from this JSON array literal:
~~~
[“Ford”, “BMW”, “Fiat”]
~~~

A
myArray = ["Ford", "BMW", "Fiat"];

//can also parse it
myJSON = '["Ford", "BMW", "Fiat"]';
myArray = JSON.parse(myJSON);
36
Q

True/False: Javascript objects can contain arrays, i.e.:
~~~
{
“name”:”John”,
“age”:30,
“cars”:[“Ford”, “BMW”, “Fiat”]
}
~~~

A

True

37
Q

How would you access an element from the cars array in the following Javascript object:
~~~
{
“name”:”John”,
“age”:30,
“cars”:[“Ford”, “BMW”, “Fiat”]
}
~~~

A

myObj.cars[0];

38
Q

How would you create a for loop to access the element values in the following Javascript object array ‘cars’?
~~~
{
“name”:”John”,
“age”:30,
“cars”:[“Ford”, “BMW”, “Fiat”]
}
~~~

A
for (let i in myObj.cars) {
  x += myObj.cars[i];
}