Syntax, scalars, collections, anchors & common patterns
Data Format# YAML = YAML Ain't Markup Language
# Superset of JSON (valid JSON is valid YAML)
# Uses indentation (spaces, NOT tabs)
# Key-value pairs
name: Alice
age: 30
active: true
# Nested (indent with 2 spaces)
address:
city: NYC
zip: "10001"
# Multiple documents in one file
---
doc1: value
---
doc2: value
...# Strings (usually no quotes needed)
name: Alice
quoted: "hello world"
single: 'no escapes here'
with_colon: "key: value" # quote if has :
# Numbers
integer: 42
float: 3.14
negative: -17
scientific: 1.2e+5
hex: 0xFF
octal: 0o77
# Boolean
enabled: true
disabled: false
# Also: yes/no, on/off (YAML 1.1)
# Null
value: null
also_null: ~
empty:# Sequence (list/array)
fruits:
- apple
- banana
- cherry
# Inline sequence
colors: [red, green, blue]
# Mapping (dict/object)
person:
name: Alice
age: 30
# Inline mapping
point: {x: 1, y: 2}
# List of objects
users:
- name: Alice
age: 30
- name: Bob
age: 25
# Nested collections
config:
db:
host: localhost
port: 5432
cache:
- redis
- memcached# Literal block (|) — preserves newlines
description: |
Line 1
Line 2
Line 3
# → "Line 1
Line 2
Line 3
"
# Folded block (>) — joins lines
summary: >
This is a long
sentence that will
be joined.
# → "This is a long sentence that will be joined.
"
# Strip trailing newline: |- >-
clean: |-
No trailing newline
# Keep trailing newlines: |+ >+
keep: |+
Extra newlines kept
# Define anchor with &
defaults: &defaults
adapter: postgres
host: localhost
port: 5432
# Reference with *
development:
database: dev_db
<<: *defaults
production:
database: prod_db
<<: *defaults
host: prod-server.com # override
# Simple anchor
color: &main_color "#3498db"
header_bg: *main_color
button_bg: *main_color# Type tags
explicit_string: !!str 42
explicit_int: !!int "42"
explicit_float: !!float "3.14"
# Dates
date: 2024-01-15
datetime: 2024-01-15T10:30:00Z
# Ordered mapping
ordered: !!omap
- first: 1
- second: 2
# Set
unique: !!set
? item1
? item2
? item3
# Complex keys
? [key, with, list]: value
? {complex: key}: value# Docker Compose
services:
web:
image: nginx
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
# GitHub Actions
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm test
# Kubernetes
apiVersion: v1
kind: Pod
metadata:
name: web
labels:
app: web