Skip to main content

YAML Indentation and Separation

Indentation and separation are two fundamental concepts for learning any programming language.

Indentation in YAML

YAML does not include mandatory spaces. Also, it is not necessary to be consistent.

An example of valid YAML indentation is shown below:

- c
- d
- e

Remember that:

  • Flow blocks must be have at least some spaces with surrounding current block level.
  • Flow content of YAML spans multiple lines and the beginning of flow content is defined with { or [.
  • Block list items include same indentation as the surrounding block level because - is considered as a part of indentation.


--- !^invoice
invoice: 123456
date : 2022-05-20
bill-to: &id001
name : Tom
surname : Nolan
lines: |
458 Walkman Dr.
Suite #292
city : New York
state : NY
postal : 10001
ship-to: *id001
- sku : BL321D
quantity : 2
description : Basketball
price : 150.00
- sku : BL1221H
quantity : 4
description : Super Shoes
price : 999.00
tax : 175.62
total: 4471.62
comments: >
Early morning is best.
Backup contact is Liam.

Don't worry if you don't understand the meaning of all the symbols.

You will learn more in the next chapters!

String Separation in YAML

Strings are separated using double-quoted string.

If you escape the newline characters in a given string, it is completely removed and translated into space value.


An example of a list of animals:

- Cat
- Dog
- Bird
- Python
- Lion
- Tiger

Note that each item is listed with a prefix of hyphen.