← VCE Software Development U4O1

Reading JSON data from an API response

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.

Understanding Reading JSON data from an API response in VCE Software Development U4O1

What it does: The 'results' key holds a list of dictionaries, so a for loop visits each one and reads its 'title' key. The response object must be converted with response.json() first, then data holds dictionaries and lists that can be read by key. Use square brackets with the key as a string. Round brackets would try to call data as a function. json() is a method, so it needs brackets. Without them you only get the method itself, not the parsed data. Accessing a key returns just the value stored under it, here the string 'Mia', not the whole dictionary. Once response.json() has produced a dictionary, you access a value by putting its key in square brackets.

Common mistakes: 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']). 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().

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 115–124) and word search (Modules 115–171).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line turns the JSON text of an API response into a Python dictionary or list?
data = response.text.dict()data = response.json()
Matches
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.
Applying
Meanings
Term
response.json()
Meaning
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.
Practice
import requests
Testing
Gaps
data = response.()
Check Answer
Spots
3data = r.json()
4print(data.name)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Cheetah' badge — Savannah Sprinter, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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() ✓
response.json() parses the JSON text and returns Python dictionaries and lists. There is no .dict() method on response.text.
2. The API returns {"city": "Perth", "temp": 31}. After data = response.json(), how do you read the temperature?
A. data['temp'] ✓
B. data.temp
The JSON object becomes a Python dictionary, so you read values with a key in square brackets: data['temp']. Dot notation does not work on dictionaries.
3. True or false: response.json() returns a Python dictionary or list, not a plain string.
A. True ✓
B. False
response.json() parses the JSON text into Python data structures. response.text is the one that gives you the raw string.
4. Which choice reads the 'name' value from the dictionary data?
A. data(name)
B. data['name'] ✓
Use square brackets with the key as a string. Round brackets would try to call data as a function.
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)
data is a list, so loop over the list first. Then use the key 'name' on each dictionary (item). A list cannot be indexed with a string key.

Match the Pairs

data = response.json() → Converts the JSON text of the API response into a Python dictionary or list stored in data
print(data['name']) → Reads the value stored under the key 'name' in the dictionary created from the JSON
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'])
After calling the API, convert the JSON response to Python data before reading any keys → response = requests.get(url) data = response.json()
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.
data = response.json() print(data.name) → Dictionary values from response.json() must be read with square brackets and a key, not with dot notation.

Key Term

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.

Fill in the Blanks

1. response = requests.get(url) data = response.json()
Hint: Type the method name that converts the response into Python dictionaries and lists.
2. data = response.json() name = data['name']
Hint: Type the variable that holds the converted dictionary, so you can read a value by its key.
3. data = response.json() for item in data['results']: print(item['name'])
Hint: Type the keyword that starts a loop over each item in the list of results.
4. data = response.json() total = len(data['results'])
Hint: Type the built-in function that counts how many items are in the results list.
5. data = response.json() first = data['results'][0] print(first['name'])
Hint: Type the index number that selects the first item in the results list.

Spot the Error

Question 1
A import requests
B r = requests.get(url)
C data = r.json()
D print(data.name)
Line 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']).
Question 2
A r = requests.get(url)
B data = r.json
C print(data['city'])
Line 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().
Question 3
A r = requests.get(url)
B data = response.json()
C print(data['temp'])
Line B — The response was stored in the variable r, so response is not defined and causes a NameError. It should be data = r.json().
Question 4
A data = r.json()
B for item in data('results'):
C print(item['title'])
Line 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']:.
Question 5
A data = r.json()
B total = 0
C for p in data['items']:
D total += data['price']
E print(total)
Line 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'].

Student with a class code? Join your class →