Python for AIPython 3.10+ · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
34 small wins to finish your pathNext lesson →

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:

python
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 file

Reading JSON text with json.loads

Example
import json

answer = '{"category": "billing", "priority": 4}'
data = json.loads(answer)

print(type(answer))
print(type(data))
print(data["priority"] + 1)

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

Example
result = {"id": 3, "category": "billing", "reply": None, "urgent": True}
print(json.dumps(result))
print(json.dumps(result, indent=2))

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:

Exampletickets.json
[
  {
    "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.

Example
with open("tickets.json") as f:
    tickets = json.load(f)

print(len(tickets))
for ticket in tickets:
    print(ticket["id"], ticket["text"])

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

Example
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())

json.dump writes to an open file, and reading the file back shows the JSON it wrote.

JSON vs Python spelling

PythonJSON
Nonenull
True, Falsetrue, 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.
Watch out. 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.
Try it yourself
  • 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.