YAML documents primarily consist of key-value pairs, where keys and values are separated by a colon (:
). It is essential to have a space after the colon to differentiate the key from the value.
Supported Data Types
- Integer: Numbers without a decimal.
- Floating Point Numbers: Numbers with a decimal.
- Strings: Text data. Important: Quote strings that contain special characters such as colons (
:
), braces ({}
), pipes (|
), or brackets ([]
). - Boolean: Values that are either true or false.
- Dates: Should follow the ISO 8601 format.
- Null Values: Represented as
null
or~
.
YAML Lists
YAML lists are denoted by a dash (-
) and are indented.
- Block Sequence: Each entry starts with a dash and a space (
-
). - Flow Sequence: Written as a comma-separated list within square brackets (
[]
).
Example
fruits:
- Apple
- Orange
- Banana
numbers: [1, 2, 3, 4]
YAML Dictionary
YAML dictionaries group key-value pairs together under a single item.
Example
person:
name: John Doe
age: 30
is_student: false
YAML Lists Containing Dictionaries
YAML allows lists to contain dictionaries, combining both structures.
Example
employees:
- name: Alice
position: Developer
- name: Bob
position: Designer
YAML Lists Containing Dictionaries Containing Lists
You can nest lists within dictionaries inside a list, creating complex structures.
Example
departments:
- name: Engineering
employees:
- Alice
- Bob
- name: Marketing
employees:
- Charlie
- Dana
YAML Pipe (|
)
The pipe notation, also known as a literal block, preserves all new lines, indentation, and extra spaces as is.
Example
description: |
This is a multiline string.
All new lines and spaces are preserved.
YAML Greater Than Sign (>
)
The greater than sign notation, also known as a folded block, renders the text as a single line. New lines are replaced with a single space, while blank lines are converted to new line characters.
Example
description: >
This is a folded
multiline string.
New lines are replaced with spaces.
YAML Comments
Comments in YAML begin with the #
sign. They are useful for adding notes or explanations within the document.
Example
# This is a comment
name: John Doe # This is an inline comment
age: 30
This guide outlines the structure and syntax of YAML, including key-value pairs, lists, dictionaries, and various notation styles for multiline text. Understanding these elements will help you effectively create and manage YAML documents.