26.2. Data Formats and JSON

In order for our application to make a request to an API, the data will need to be formatted in a way both our application and the API can understand.

The API may have been built with another programming language. And it may not use the same variables, objects, and data structures as JavaScript. To solve the issue of APIs being built in different programming languages, data formats are used.

A data format is a set of rules that govern how data is written, organized, and labeled. Data formats make working with data consistent and reliable.

26.2.1. JSON

There are quite a few different data formats, but we will only focus on one throughout this class: JavaScript Object Notation, also known as JSON. JSON is one of the leading data formats used, especially on the web.

JSON is based on JavaScript object syntax, but has some differences.

Let's consider an API that serves information about the books in a library. In this example, we searched for "An Astronaut's Guide to Life on Earth".

1{
2    "title": "An Astronaut's Guide to Life on Earth",
3    "author": "Chris Hadfield",
4    "ISBN": 9780316253017,
5    "year_published": 2013,
6    "subject": ["Hadfield, Chris", "Astronauts", "Biography"],
7    "available": true
8}

The API returned a match for our search. The search provides us with information that may be useful to the user in the form of the title, author, ISBN, the year the book was published, the subjects of the book, and if the book is currently available for checkout.

26.2.2. JSON Rules

JSON is a collection of key-value pairs. In the example above, "title" is a key and it's value is "An Astronaut's Guide to Life on Earth".

The key-value pairs describe the data that is being transferred.

A JSON key MUST be a string, but the value may be a number, string, boolean, array, object, or null.

In the example above, the JSON describes one object, a book! All of the keys are strings, and the values are: string, string, number, number, array, and boolean respectively.

JSON can also be used to describe a collection of objects at the same time. Consider we search for the word "Astronaut".

 1{
 2    "hits": 3,
 3    "book": [
 4        {
 5            "title": "An Astronaut's Guide to Life on Earth",
 6             "author": "Chris Hadfield",
 7             "ISBN": 9780316253017,
 8             "year_published": 2013,
 9             "subject": ["Hadfield, Chris", "Astronauts", "Biography"],
10             "available": true
11        },
12        {
13            "title": "Astronaut",
14            "author": "Lucy M. George",
15            "ISBN": 9781609929411,
16            "year_published": 2016,
17            "subject": ["Astronauts", "Juvenile Fiction", "Space stations"],
18            "available": false
19        },
20        {
21            "title": "Astronaut Ellen Ochoa",
22            "author": "Heather E. Schwartz",
23            "ISBN": 9781512434491,
24            "year_published": 2018,
25            "subject": ["Ochoa Ellen", "Women astronauts", "Astronauts", "Biography", "Women scientists", "Hispanic American women"],
26            "available": true
27        }
28    ]
29}

This time, our search term "Astronaut" returned multiple books, and so a collection of book objects was returned in JSON format.

Each book object can be found in the array with the key "book". Each book contains the keys "title", "author", "ISBN", "year_published", "subject", and "available".

When we make a request to an API, the API formats the data we requested into JSON and then responds to our request with the JSON representation of our request.

26.2.3. JSON & JavaScript Object Differences

JSON is rooted in JavaScript objects syntax. However, there are some key differences between the two.

JSON keys MUST be in double quotes. Double quotes should not be used when declaring properties for a JavaScript object.

JSON:

1{
2    "title": "The Cat in the Hat",
3    "author": "Dr. Seuss"
4}

JavaScript object:

1let newBook = {
2    title: "The Cat in the Hat",
3    author: "Dr. Seuss"
4}

To represent a string in JSON, you MUST use double quotes. In JavaScript, you can use double quotes or single quotes.

JSON:

1{
2    "title": "The Last Astronaut",
3    "author": "David Wellington"
4}

JavaScript object:

1let anotherBook = {
2    title: 'The Last Astronaut',
3    author: 'David Wellington'
4}

Note

JSON is based on JavaScript objects, but there are key differences. JSON syntax is a little more strict than JavaScript object syntax.

26.2.4. Check Your Understanding

Question

What does API stand for?

Question

Why might you connect to an API?

Question

True or False: JSON is JavaScript.

Question

What purpose does JSON serve?