mozilla Flashcards

1
Q

What is JSON?

A

A text-based data format following JavaScript object syntax. A standard text-based format for representing structured data based on JavaScript object syntax.

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

Who was JSON popularized by?

A

Douglas Crockford.

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

What is JSON commonly used for?

A

Transmitting data in web applications.

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

Give an example of how JSON can be used to transmit data in web application.

A

Sending some data from the server to the client, so it can be displayed on a web page, or vice versa

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

What does the JSON. parse() method do?

A

Constructs the JavaScript value or object described by the string.

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

What should you note about JSON even though it closely resembles JavaScript object literal syntax?

A

It can be used independently from JavaScript, and many programming environments feature the ability to read (parse) and generate JSON.

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

How does JSON exist?

A

As a string — useful when you want to transmit data across a network.

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

What do you need to do if you want to access the JSON data?

A

Convert it to a native JavaScript object.

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

Is converting JSON into a JavaScript object difficult?

A

No, JavaScript provides a global JSON object that has methods available for converting between the two.

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

What is converting a string to a native object is called?

A

Deserialization.

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

What is converting a native object to a string so it can be transmitted across the network is called?

A

Serialization.

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

Where can a JSON object be stored?

A

In its own file, which is basically just a text file with an extension of .json, and a MIME type of application/json.

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

What data types can you include in JSON?

A

The same basic data types as you can in a standard JavaScript object — strings, numbers, arrays, booleans, and other object literals.

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

What’s one way JSON differs from a JavaScript object?

A

An array is also valid JSON.

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

Does JSON contain anything other than data?

A

No, JSON is purely a data format — it contains only properties, no methods.

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

What is something JSON requires?

A

Double quotes to be used around strings and property names. Single quotes are not valid.

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

How can you validate JSON?

A

Using an application like JSONLint.

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

Why is it a good idea to validate JSON using an application like JSONLint?

A

Because even a single misplaced comma or colon can cause a JSON file to go wrong, and not work. You should be careful to validate any data you are attempting to use (although computer-generated JSON is less likely to include errors, as long as the generator program is working correctly).

20
Q

What other forms can JSON take apart from arrays and objects?

A

Any data type that is valid for inclusion inside JSON. For example, a single string or number would be a valid JSON object.

21
Q

In JavaScript code,object properties may be unquoted. How does this differ to JSON?

A

Only quoted strings may be used as properties in JSON.

22
Q

What is a way of obtaining JSON?

A

Use an API called XMLHttpRequest (often called XHR).

23
Q

What is XMLHttpRequest (often called XHR)?

A

A very useful JavaScript object that allows us to make network requests to retrieve resources from a server via JavaScript (e.g. images, text, JSON, even HTML snippets).

24
Q

What advantage is there of making network requests to retrieve resources from a server via JavaScript?

A

We can update small sections of content without having to reload the entire page.

25
Q

Why is it better to use XHR to update small sections of content without having to reload the entire page?

A

It allows for more responsive web pages.

26
Q

What are the steps for using some JSON data on a website?

A

Store the URL of the JSON we want to retrieve in a variable.
Create a request, we need to create a new request object instance from the XMLHttpRequest constructor, using the new keyword.
Open the request using the open() method.
Set the responseType to JSON, so that XHR knows that the server will be returning JSON, and that this should be converted behind the scenes into a JavaScript object. Then we send the request with the send() method.
And lastly, wait for the response to return from the server, and deal with it.

27
Q

How would you store the URL of the JSON you want to retrieve in a variable?

A

let requestURL = ‘https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json’

28
Q

How would you create a request, by creating a new request object instance from the XMLHttpRequest constructor, using the new keyword?

A

let request = new XMLHttpRequest();

29
Q

How would you open the request using the open() method?

A

request.open(‘GET’, requestURL);

This takes at least two parameters — there are other optional parameters available. You only need the two mandatory ones for a simple example.

30
Q

What are the parameters in this example?

request.open(‘GET’, requestURL);

A

The HTTP method to use when making the network request. In this case GET is fine, as we are just retrieving some simple data.
The URL to make the request to — this is the URL of the JSON file that we stored earlier.

31
Q

How would you set the response type and and send the request?

A

request. responseType = ‘json’;

request. send();

32
Q

How would you deal with the response from the server?

A
request.onload = function() {
  const superHeroes = request.response;
  populateHeader(superHeroes);
  showHeroes(superHeroes);
}
33
Q

Where can we access the response of the request?

A

On the response property.

34
Q

What does the variable superHeroes contain in this code?

request.onload = function() {
  const superHeroes = request.response;
  populateHeader(superHeroes);
  showHeroes(superHeroes);
}
A

The JavaScript object based on the JSON.

35
Q

What are we doing to the JavaScript object in this code?

request.onload = function() {
  const superHeroes = request.response;
  populateHeader(superHeroes);
  showHeroes(superHeroes);
}
A

Passing that object to two function calls — the first one fills the with the correct data, while the second one creates an information card for each hero on the team, and inserts it into the .

36
Q

Why would you wrap the code in an event handler that runs when the load event fires on the request object?

A

Because the load event fires when the response has successfully returned; doing it this way guarantees that request.response will definitely be available when we come to try to do something with it.

37
Q
request.onload = function() {
  const superHeroes = request.response;
  populateHeader(superHeroes);
  showHeroes(superHeroes);
}

Create two functions where the first one fills a with the correct data, while the second one creates an information card for each hero on the team, and inserts it into the

A
function populateHeader(jsonObj) {
  const myH1 = document.createElement('h1');
  myH1.textContent = jsonObj['squadName'];
  header.appendChild(myH1);
  const myPara = document.createElement('p');
  myPara.textContent = 'Hometown: ' + jsonObj['homeTown'] + ' // Formed: ' + jsonObj['formed'];
  header.appendChild(myPara);
}
38
Q

Why was the parameter named jsonObj in this code?

function populateHeader(jsonObj) {
  const myH1 = document.createElement('h1');
  myH1.textContent = jsonObj['squadName'];
  header.appendChild(myH1);
  const myPara = document.createElement('p');
  myPara.textContent = 'Hometown: ' + jsonObj['homeTown'] + ' // Formed: ' + jsonObj['formed'];
  header.appendChild(myPara);
}
A

To remind ourselves that this JavaScript object originated from JSON.

39
Q

Some code that displays the superhero cards:

A
function showHeroes(jsonObj) {
  const heroes = jsonObj['members'];
  for (let i = 0; i < heroes.length; i++) {
    const myArticle = document.createElement('article');
    const myH2 = document.createElement('h2');
    const myPara1 = document.createElement('p');
    const myPara2 = document.createElement('p');
    const myPara3 = document.createElement('p');
    const myList = document.createElement('ul');
myH2.textContent = heroes[i].name;
myPara1.textContent = 'Secret identity: ' + heroes[i].secretIdentity;
myPara2.textContent = 'Age: ' + heroes[i].age;
myPara3.textContent = 'Superpowers:';
    const superPowers = heroes[i].powers;
    for (let j = 0; j < superPowers.length; j++) {
      const listItem = document.createElement('li');
      listItem.textContent = superPowers[j];
      myList.appendChild(listItem);
    }
    myArticle.appendChild(myH2);
    myArticle.appendChild(myPara1);
    myArticle.appendChild(myPara2);
    myArticle.appendChild(myPara3);
    myArticle.appendChild(myList);
section.appendChild(myArticle);   } }
40
Q

What does the parse() method do?

A

Accepts a JSON string as a parameter, and returns the corresponding JavaScript object.

41
Q

What does the stringify() methods do?

A

Accepts an object as a parameter, and returns the equivalent JSON string form.

42
Q

What is noteworthy about the methods parse() and stringify()

A

A built-in JSON object is available in browsers, which contains these two methods.

43
Q

Show an example of how you would handle receiving raw JSON text.

A

request. open(‘GET’, requestURL);
request. responseType = ‘text’; // now we’re getting a string!
request. send();

request.onload = function() {
const superHeroesText = request.response; // get the string from the response
const superHeroes = JSON.parse(superHeroesText); // convert it to an object
populateHeader(superHeroes);
showHeroes(superHeroes);
}

44
Q

Show an example using stringify()

A
let myJSON = { "name": "Chris", "age": "38" };
let myString = JSON.stringify(myJSON);
45
Q

What is happening with this code?

let myJSON = { "name": "Chris", "age": "38" };
let myString = JSON.stringify(myJSON);
A

We’re creating a JavaScript object, then converting it to a JSON string using stringify() and saving the return value in a new variable.