JSON
JSON is a text format for data that looks almost like Python lists and dictionaries, and the json module converts between the two.
Last updated: 30 Sep, 2026 · Python 3.14
To use a model's answer rather than only display it, you ask the model for JSON. Files, from Reading and writing files, often hold JSON too.
Syntax:
json.loads(text) # JSON text -> Python value
json.dumps(value) # Python value -> JSON text
json.load(f) # from an open file
json.dump(value, f) # to an open fileReading JSON text with json.loads
import json
answer = '{"category": "billing", "priority": 4}'
data = json.loads(answer)
print(type(answer))
print(type(data))
print(data["priority"] + 1)<class 'str'> <class 'dict'> 5
answer is a string that looks like a dictionary. json.loads, load from a string, reads it and builds the real dictionary, so data["priority"] is a number you can add to.
Writing JSON text with json.dumps
result = {"id": 3, "category": "billing", "reply": None, "urgent": True}
print(json.dumps(result))
print(json.dumps(result, indent=2)){"id": 3, "category": "billing", "reply": null, "urgent": true}
{
"id": 3,
"category": "billing",
"reply": null,
"urgent": true
}json.dumps goes the other way. Look at what changed: JSON writes None as null and True as true, and always uses double quotes. indent=2 spreads it over lines for people to read.
Loading the tickets file
The five tickets the rest of the course uses live in a file:
[
{
"id": 1,
"customer": "Asha",
"text": "I was charged twice for one order"
},
{
"id": 2,
"customer": "Ben",
"text": "My parcel has not arrived"
},
{
"id": 3,
"customer": "Chen",
"text": "Can I get a refund for the blue mug?"
},
{
"id": 4,
"customer": "Dara",
"text": "How do I change my password?"
},
{
"id": 5,
"customer": "Eli",
"text": "The parcel arrived but the box was crushed"
}
]Five dictionaries in a list, each with an id, a customer and the text they wrote. Reading the file gives that list back.
with open("tickets.json") as f:
tickets = json.load(f)
print(len(tickets))
for ticket in tickets:
print(ticket["id"], ticket["text"])5 1 I was charged twice for one order 2 My parcel has not arrived 3 Can I get a refund for the blue mug? 4 How do I change my password? 5 The parcel arrived but the box was crushed
json.load, without the s, reads from an open file. The file becomes a list of dictionaries, the shape from Lists of dictionaries.
Saving a summary with json.dump
counts = {"tickets": len(tickets), "customers": [t["customer"] for t in tickets]}
with open("summary.json", "w") as f:
json.dump(counts, f, indent=2)
with open("summary.json") as f:
print(f.read()){
"tickets": 5,
"customers": [
"Asha",
"Ben",
"Chen",
"Dara",
"Eli"
]
}json.dump writes to an open file, and reading the file back shows the JSON it wrote.
JSON vs Python spelling
| Python | JSON |
|---|---|
None | null |
True, False | true, false |
'single' or "double" quotes | "double" quotes only |
Where JSON shows up in AI code
- Asking a model to answer in JSON, then reading the answer with
json.loads. - The body of almost every web API request and response.
- Saving results, settings and test cases to files.
json.loads takes a string and json.load takes an open file. Passing a file to loads is a TypeError; the s stands for string.Related
- Previous: Reading and writing files
- Next: Stand-in model
- Reference: The json module in the Python docs
- Add a sixth ticket to the list after loading, and write the whole list to
tickets2.json. - Run
json.loads("{'category': 'billing'}"), with single quotes inside, and read the error. - Print
json.dumps([1, "two", None]).
You understood something today that you didn't yesterday.