Skip to main content

How to Nest Settings Inside Other Settings in YAML

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Settings that belong together go under one parent key in YAML, indented below it: a store's name and its address, and inside the address, the city and the postcode. The indentation is the structure, so it has to be even. This guide nests two levels deep and checks the result with Python.

Syntax​

Syntax
<parent>:
<key>: <value>
<child>:
<key>: <value>
  • <parent>: with nothing after the colon: the settings below belong to it.
  • The indentation: spaces at the start of the line. Every line in the same group has the same number of spaces; two per level is common.
  • <child>:: a group inside a group, indented further.

The YAML 1.2.2 specification says: "In YAML block styles, structure is determined by indentation. In general, indentation is defined as a zero or more space characters at the start of a line."

The Example​

store.yaml describes a bike shop: a store group with a name and an address group, which holds a city and a postcode.

Prerequisites​

  • uv and Python, with PyYAML in a project (uv add pyyaml), to check the file. This guide uses PyYAML 6.0.4 on Python 3.15.2.

Step 1: Write the File​

store.yaml
store:
name: Bike shop
address:
city: Leeds
postcode: LS1 1AA

store.yaml in a macOS-style code window: store with name Bike shop and an address group that holds city Leeds and postcode LS1 1AA, each level indented by two more spaces.

Step 2: Check It with PyYAML​

Save this checker as show.py. It loads any YAML file you give it and prints the result:

show.py
import sys
from pprint import pp

import yaml

with open(sys.argv[1], encoding="utf-8") as file:
pp(yaml.safe_load(file))
Terminal
uv run python show.py store.yaml
Output
{'store': {'name': 'Bike shop',
'address': {'city': 'Leeds', 'postcode': 'LS1 1AA'}}}

The terminal after uv run python show.py store.yaml: a store dictionary holding the name and an address dictionary with city and postcode.

Each level of indentation became a dictionary inside a dictionary. In Python, the city is data["store"]["address"]["city"]. The command works unchanged in PowerShell.

Common Mistakes​

MistakeExampleWhat PyYAML did
Uneven indentation in one groupname indented by two spaces and city by three, both under storemapping values are not allowed here, at line 3, column 8
Indenting with a tabA tab before name under storefound character '\t' that cannot start any token, at line 2, column 1

Troubleshooting​

SymptomLikely causeFix
mapping values are not allowed hereA line is indented differently from the others in its groupLine up every key of the group with the same number of spaces
A key ends up in the wrong groupIt's indented to a different levelCount the spaces and match the group it belongs to

FAQ​

How many spaces should I use?​

Any number, as long as every key in a group uses the same: the same file indented by four spaces loaded the same way. Two spaces per level is the common choice.

What does the parent key's value hold?​

The whole group below it: store holds a dictionary with name and address.

Conclusion​

Indenting name and address under store, and city and postcode under address, gave dictionaries inside dictionaries. Keep each group's indentation identical, and use spaces only.

Additional Resources​