Reading JSON data from an API response

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Reading JSON data from an API response

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line turns the JSON text of an API response into a Python dictionary or list?
A data = response.text.dict()
B data = response.json()
2. The API returns {"city": "Perth", "temp": 31}. After data = response.json(), how do you read the temperature?
A data['temp']
B data.temp
3. True or false: response.json() returns a Python dictionary or list, not a plain string.
A True
B False
4. Which choice reads the 'name' value from the dictionary data?
A data(name)
B data['name']
5. The JSON response is a list of results: [{"name": "Ava"}, {"name": "Liam"}]. How do you print every name?
A for item in data: print(item['name'])
B for item in data['name']: print(item)

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1data = response.json() Aresponse = requests.get(url) data = response.json()
2print(data['name']) Bfor item in data['results']: print(item['title'])
3Looping over a list of results in the JSON data so each item can be read by key CDictionary values from response.json() must be read with square brackets and a key, not with dot notation.
4After calling the API, convert the JSON response to Python data before reading any keys DReads the value stored under the key 'name' in the dictionary created from the JSON
5data = response.json print(data['name']) EConverts the JSON text of the API response into a Python dictionary or list stored in data
6data = response.json() print(data.name) Fresponse.json is missing its brackets, so it stores the method itself instead of calling it and the JSON is never converted.

Definitions

response.json()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
response = requests.get(url) data = response.____()
2.
data = response.json() name = ____['name']
3.
data = response.json() ___ item in data['results']: print(item['name'])
4.
data = response.json() total = ___(data['results'])
5.
data = response.json() first = data['results'][_] print(first['name'])

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A import requests
B r = requests.get(url)
C data = r.json()
D print(data.name)
Question 2
A r = requests.get(url)
B data = r.json
C print(data['city'])
Question 3
A r = requests.get(url)
B data = response.json()
C print(data['temp'])
Question 4
A data = r.json()
B for item in data('results'):
C print(item['title'])
Question 5
A data = r.json()
B total = 0
C for p in data['items']:
D total += data['price']
E print(total)

Answer Key

Reading JSON data from an API response
Section A: Quick Questions
Q1
B — data = response.json()
Q2
A — data['temp']
Q3
A — True
Q4
B — data['name']
Q5
A — for item in data: print(item['name'])
Section B: Match the Pairs
Q1
data = response.json() → Converts the JSON text of the API response into a Python dictionary or list stored in data
Q2
print(data['name']) → Reads the value stored under the key 'name' in the dictionary created from the JSON
Q3
Looping over a list of results in the JSON data so each item can be read by key → for item in data['results']: print(item['title'])
Q4
After calling the API, convert the JSON response to Python data before reading any keys → response = requests.get(url) data = response.json()
Q5
data = response.json print(data['name']) → response.json is missing its brackets, so it stores the method itself instead of calling it and the JSON is never converted.
Q6
data = response.json() print(data.name) → Dictionary values from response.json() must be read with square brackets and a key, not with dot notation.
Section C: Definitions
Q1
response.json() — Converts the JSON text of an API response into Python dictionaries and lists, so you can read values by key, e.g. data['name'], or loop over a list of results.
Section D: Fill in the Blanks
Q1
response = requests.get(url) data = response.json()
Q2
data = response.json() name = data['name']
Q3
data = response.json() for item in data['results']: print(item['name'])
Q4
data = response.json() total = len(data['results'])
Q5
data = response.json() first = data['results'][0] print(first['name'])
Section E: Spot the Error
Q1
D — response.json() gives a dictionary, and dictionary values are read by key with square brackets. Dot notation does not work on a dictionary. It should be print(data['name']).
Q2
B — json is a method, so it needs brackets to run. Without them, data just holds the method itself instead of the dictionary. It should be data = r.json().
Q3
B — The response was stored in the variable r, so response is not defined and causes a NameError. It should be data = r.json().
Q4
B — data is a dictionary, not a function, so it cannot be called with round brackets. A key is read with square brackets. It should be for item in data['results']:.
Q5
D — data is the whole dictionary and has no 'price' key. The price belongs to each item, so the loop variable p must be used: total += p['price'].