Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Data in JSON and YAML

After this lesson: I can read and write data in JSON and YAML.

50 min lessonNo commands

An order a person can read

Priya orders two teas and a cake on a shop's website. The website sends her order to the kitchen's system.

Both programs must understand it. A developer fixing a fault at midnight must read it too.

So the order travels as plain text in an agreed shape.

By the end of this lesson

Note

I can read and write data in JSON and YAML.

Key words

WordWhat it meansExample
JSONa text format for data, built from objects and lists
Objecta group of keys, each with a value{"qty": 2}
Keythe name of one value in an object"qty"
Listvalues in order, inside square brackets["tea", "cake"]
Nestingputting an object or list inside another
YAMLa format for the same data that uses indentation

The cells students fill in are left blank in this preview.

JSON is objects and lists

Curly brackets { } hold an object: keys and values, such as "customer": "Priya".

Square brackets [ ] hold a list. Text goes in double quotes. Numbers have no quotes.

Yes and no are true and false. In Python, json.loads turns JSON text into dictionaries and lists.

Read an order

Code: python
import json

# An order, as the shop's website sends it.
text = '{"id": 1042, "customer": "Priya", "paid": true, "items": [{"name": "Tea", "qty": 2}, {"name": "Cake", "qty": 1}]}'

order = json.loads(text)
print(order["customer"])
print(order["paid"])
print(order["items"][0]["name"])

# Your turn: print the qty of the cake.

Follow nesting one step at a time

The order holds a list called items. Each item is an object of its own. That is nesting.

To reach a value, follow the path one step at a time. order["items"] is the list. [0] is the first item, because counting starts at 0. ["name"] is its name.

Worked example: paths into the order

Path in PythonWhat it gives
order["id"]1042
order["items"]The list of two items
order["items"][0]{"name": "Tea", "qty": 2}
order["items"][0]["qty"]2
order["items"][1]["name"]Cake

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think JSON accepts single quotes, because Python does. It does not: {'qty': 2} makes json.loads fail. Use double quotes, and put no comma after the last item.

Write a support ticket as JSON

Code: python
import json

ticket = {
    "id": 77,
    "subject": "Payment failed",
    "tags": ["payments", "urgent"],
    "customer": {"name": "Tom", "city": "Leeds"},
}

# indent=2 makes the JSON easy for a person to read.
print(json.dumps(ticket, indent=2))

# Your turn: add "open": True to the ticket and run it again.
# Notice that Python's True is written true in JSON.

YAML says the same with indentation

YAML holds the same objects and lists with fewer brackets. A key and its value sit on one line: name: Tom.

A list item starts with - . Nesting is shown by indenting two spaces.

You meet YAML in settings files for servers and build tools. The spaces matter, so never use a tab.

The ticket in YAML

Code: bash
# The same ticket in YAML
id: 77
subject: Payment failed
tags:
  - payments
  - urgent
customer:
  name: Tom
  city: Leeds

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.