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:

a:
b:
- c
- d
- e
f:
"hello!"

Remember that:

  • Flow blocks must 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.

Example

--- !tutorialreference.com/^invoice
invoice: 123456
date : 2022-05-20
bill-to: &id001
name : Tom
surname : Nolan
address:
lines: |
458 Walkman Dr.
Suite #292
city : New York
state : NY
postal : 10001
ship-to: *id001
product:
- 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.
note

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.

Example

An example of a list of animals:

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

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