← QCE Digital Solutions Unit 4

Creating JSON text with 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.

Understanding Creating JSON text with json.dumps in QCE Digital Solutions Unit 4

What it does: A Python list passed to json.dumps() becomes a JSON array written in square brackets: [1, 2, 3]. JSON objects use braces around key-value pairs, and every key is wrapped in double quotes. json.dumps() returns a string, which is why it is ready to send in a request. Use json.loads() to get a dictionary back.

Common mistakes: 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}. 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).

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 334–401) and word search (Modules 127–401).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which call turns the Python dictionary student into JSON text ready to send?
json.dumps(student)json.loads(student)
Matches
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.
Applying
Meanings
Term
json.dumps()
Meaning
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.
Practice
import json
Testing
Gaps
import
Check Answer
Spots
1import json
2p = ["name": "Ava", "yr": 12]

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

The 'Gazelle' badge — Tuple 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 call turns the Python dictionary student into JSON text ready to send?
A. json.loads(student)
B. json.dumps(student) ✓
json.dumps() converts Python data into JSON text. json.loads() goes the other way, from JSON text back to Python data.
2. Which line turns the JSON text reply back into a Python dictionary?
A. data = json.dumps(reply)
B. data = json.loads(reply) ✓
json.loads() reads JSON text and gives you Python data. json.dumps() would only make more JSON text.
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
JSON always uses double quotes around keys and text values, even if the Python code used single quotes.
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}
Lists become JSON arrays in square brackets. Dictionaries become JSON objects in braces, made of key-value pairs.
5. What type of value does json.dumps() return?
A. A string of JSON text ✓
B. A Python dictionary
json.dumps() returns a string, which is why it is ready to send in a request. Use json.loads() to get a dictionary back.

Match the Pairs

json.dumps({"name": "Ava"}) → Turns a Python dictionary into JSON text that can be sent
json.loads('{"score": 90}') → Turns JSON text back into a Python dictionary
json.dumps() turns a list into JSON text with square brackets → json.dumps([1, 2, 3])
JSON key-value pairs sit inside braces and the keys use double quotes → {"city": "Brisbane", "temp": 28}
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.
{'name': 'Ava', 'age': 17} → The keys and strings use single quotes. Valid JSON text needs double quotes: {"name": "Ava", "age": 17}.

Key Term

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.

Fill in the Blanks

1. import json user = {"name": "Mia", "year": 12} text = json.dumps(user)
Hint: Type the name of the standard module that must be imported to work with JSON.
2. user = {"name": "Mia", "year": 12} text = json.dumps(user) print(text)
Hint: Type the json function that turns a Python dictionary into JSON text.
3. text = '{"name": "Mia", "year": 12}' data = json.loads(text) print(data)
Hint: Type the json function that turns JSON text back into Python data.
4. scores = [88, 74, 91] text = json.dumps(scores) print(text)
Hint: Type the variable that holds the list you want to convert to JSON text.
5. user = {"name": "Mia", "year": 12} payload = json.dumps(user) print(type(payload))
Hint: Type the built-in function that reveals what data type payload is.

Spot the Error

Question 1
A import json
B p = ["name": "Ava", "yr": 12]
C text = json.dumps(p)
D print(text)
Line 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}.
Question 2
A import json
B scores = [90, 85, 72]
C text = json.dump(scores)
D print(text)
Line 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).
Question 3
A import json
B user = {"id": 7}
C text = json.dumps(user)
D back = json.loads(user)
Line 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).
Question 4
A import json
B box = {"a" = 1, "b" = 2}
C text = json.dumps(box)
D print(text)
Line B — A key and its value are separated by a colon, not an equals sign. It should be box = {"a": 1, "b": 2}.
Question 5
A import jsn
B pet = {"type": "cat"}
C text = json.dumps(pet)
D print(text)
Line A — The module is called json. Misspelling it means Python cannot find it, and json.dumps() would not work. It should be import json.

Student with a class code? Join your class →