Creating JSON text with json.dumps

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

Creating JSON text with json.dumps

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which call turns the Python dictionary student into JSON text ready to send?
A json.loads(student)
B json.dumps(student)
2. Which line turns the JSON text reply back into a Python dictionary?
A data = json.dumps(reply)
B data = json.loads(reply)
3. What does json.dumps({'name': 'Ava'}) give you?
A The text {"name": "Ava"} with double-quoted keys
B The text {'name': 'Ava'} with single-quoted keys
4. Which Python value does json.dumps() turn into JSON square brackets [ ]?
A A list such as [1, 2, 3]
B A dictionary such as {'a': 1}
5. What type of value does json.dumps() return?
A A string of JSON text
B A Python dictionary

Match the Pairs

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

Left columnRight column
1json.dumps({"name": "Ava"}) A{"city": "Brisbane", "temp": 28}
2json.loads('{"score": 90}') BTurns JSON text back into a Python dictionary
3json.dumps() turns a list into JSON text with square brackets Cjson.dumps() was used to turn JSON text back into Python data. It should be json.loads(data), because dumps() only creates JSON text.
4JSON key-value pairs sit inside braces and the keys use double quotes DThe keys and strings use single quotes. Valid JSON text needs double quotes: {"name": "Ava", "age": 17}.
5data = json.dumps(text) person = json.dumps(data) ETurns a Python dictionary into JSON text that can be sent
6{'name': 'Ava', 'age': 17} Fjson.dumps([1, 2, 3])

Definitions

json.dumps()

Fill in the Blanks

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

1.
import ____ user = {"name": "Mia", "year": 12} text = json.dumps(user)
2.
user = {"name": "Mia", "year": 12} text = json._____(user) print(text)
3.
text = '{"name": "Mia", "year": 12}' data = json._____(text) print(data)
4.
scores = [88, 74, 91] text = json.dumps(______) print(text)
5.
user = {"name": "Mia", "year": 12} payload = json.dumps(user) print(____(payload))

Spot the Error

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

Question 1
A import json
B p = ["name": "Ava", "yr": 12]
C text = json.dumps(p)
D print(text)
Question 2
A import json
B scores = [90, 85, 72]
C text = json.dump(scores)
D print(text)
Question 3
A import json
B user = {"id": 7}
C text = json.dumps(user)
D back = json.loads(user)
Question 4
A import json
B box = {"a" = 1, "b" = 2}
C text = json.dumps(box)
D print(text)
Question 5
A import jsn
B pet = {"type": "cat"}
C text = json.dumps(pet)
D print(text)

Answer Key

Creating JSON text with json.dumps
Section A: Quick Questions
Q1
B — json.dumps(student)
Q2
B — data = json.loads(reply)
Q3
A — The text {"name": "Ava"} with double-quoted keys
Q4
A — A list such as [1, 2, 3]
Q5
A — A string of JSON text
Section B: Match the Pairs
Q1
json.dumps({"name": "Ava"}) → Turns a Python dictionary into JSON text that can be sent
Q2
json.loads('{"score": 90}') → Turns JSON text back into a Python dictionary
Q3
json.dumps() turns a list into JSON text with square brackets → json.dumps([1, 2, 3])
Q4
JSON key-value pairs sit inside braces and the keys use double quotes → {"city": "Brisbane", "temp": 28}
Q5
data = json.dumps(text) person = json.dumps(data) → json.dumps() was used to turn JSON text back into Python data. It should be json.loads(data), because dumps() only creates JSON text.
Q6
{'name': 'Ava', 'age': 17} → The keys and strings use single quotes. Valid JSON text needs double quotes: {"name": "Ava", "age": 17}.
Section C: Definitions
Q1
json.dumps() — Turns a Python dictionary or list into JSON text (double-quoted keys, braces, square brackets) ready to send. json.loads() does the reverse and turns JSON text back into Python data.
Section D: Fill in the Blanks
Q1
import json user = {"name": "Mia", "year": 12} text = json.dumps(user)
Q2
user = {"name": "Mia", "year": 12} text = json.dumps(user) print(text)
Q3
text = '{"name": "Mia", "year": 12}' data = json.loads(text) print(data)
Q4
scores = [88, 74, 91] text = json.dumps(scores) print(text)
Q5
user = {"name": "Mia", "year": 12} payload = json.dumps(user) print(type(payload))
Section E: Spot the Error
Q1
B — Key-value pairs go inside braces { }, not square brackets. Square brackets are for arrays (lists) and cannot hold name: value pairs. It should be p = {"name": "Ava", "yr": 12}.
Q2
C — json.dump() writes to a file and needs a file argument. To turn a list into a JSON string, use json.dumps() (with an s for string): text = json.dumps(scores).
Q3
D — json.loads() reads JSON text (a string), not a Python dictionary. It must be given the JSON string made by dumps: back = json.loads(text).
Q4
B — A key and its value are separated by a colon, not an equals sign. It should be box = {"a": 1, "b": 2}.
Q5
A — The module is called json. Misspelling it means Python cannot find it, and json.dumps() would not work. It should be import json.