Basic YAML Syntax and Data Types
✍️ YAML Syntax & Data Types
Understanding YAML syntax and data types is fundamental to writing effective YAML documents.
🔹 Core Data Types
🔤 Scalars
- Strings: Can be unquoted or quoted (
'single'
or"double"
) - Numbers: Integers, floats
- Booleans:
true
,false
- Null:
null
or~
📋 Sequences (Lists)
Represented as lists, indicated by dashes:
fruits:
- Apple
- Banana
- Cherry
🔑 Mappings (Key-Value Pairs)
Use colons to separate keys and values:
person:
name: Alice
age: 28
city: Wonderland
🧱 Indentation
- Uses spaces only (tabs are discouraged)
- Preferably 2 or 4 spaces
- Consistent indentation is crucial for valid YAML
🧾 Multi-line Scalars
Use block style:
|
for literal (preserves line breaks)>
for folded (folds into a single line)
description: |
This is a multiline
string that preserves line breaks.
♻️ Anchors & Aliases
Enable reuse of data snippets:
defaults: &defaults
timeout: 30
retries: 3
service:
<<: *defaults
name: MyService
✅ This supports DRY (Don't Repeat Yourself) principles in YAML documents.