Getting Started
Basic Structure
YAML uses indentation (spaces, not tabs) for nesting. Mappings use key: value, sequences use - item. Inline syntax uses [] for lists and {} for maps. Comments use #.
# YAML uses indentation for structure
# Comments start with #
# Key-value pairs (mappings)
name: Alice
age: 30
# Nested mappings (use consistent indentation)
person:
name: Bob
age: 25
address:
city: NYC
zip: 10001
# Sequences (lists)
hobbies:
- reading
- coding
- music
# Inline syntax
numbers: [1, 2, 3]
coords: {x: 10, y: 20}Comments
Comments start with # and must be preceded by whitespace or start of line. Inside block scalars (| and >), # is literal content, not a comment. There is no multi-line comment syntax — each line needs its own #.
# Full-line comment
key: value # Inline comment (whitespace before # required)
# Comments can appear anywhere
# - before documents
# - inside sequences
list:
- a # first item
- b # second item
# Block scalar headers DO accept comments
text: | # literal block
line one
# But the content of block scalars is literal,
# so # inside is NOT a comment:
literal: |
# this is part of the string, not a commentIndentation Rules
YAML strictly forbids tabs for indentation — only spaces are allowed. Indentation must be consistent within a level but doesn't need to be a fixed number. The - in sequences is part of the indentation. Mixing tabs and spaces throws a parser error.
# Use SPACES only — tabs are forbidden for indentation
# 2 spaces is the most common convention
mapping:
nested:
deeply_nested: value
# Sequences can be indented at the same level as the key
items:
- one
- two
# Or indented further (both are valid)
items2:
- one
- two
# WRONG: tab characters cause parser errors
# bad: value (tab before value)
# Hyphen counts as indentation start
- item1
- item2Document Start & End
--- marks the start of a YAML document; ... marks the end. A file can contain multiple documents separated by --- (a document stream). For single-document config files, a leading --- is optional but recommended as a hint to parsers.
# --- separates documents in a stream
# ... ends a document (optional)
---
name: first document
value: 1
...
---
name: second document
value: 2
...
# A single document needs no markers
key: value
# But --- is recommended at the start of files
# to disambiguate from plain text
---
apiVersion: v1
kind: ConfigMapCommon Pitfalls
The colon needs a following space to be a mapping separator. YAML 1.1 vs 1.2 differ on yes/no/on/off (booleans in 1.1, strings in 1.2) and on octal literals. Empty values are null, distinct from empty strings. Many parsers (PyYAML) still follow 1.1 rules.
# Colon needs a space after it
key:value # WRONG: parsed as a single string "key:value"
key: value # correct
# Yes/No/On/Off are NOT booleans in YAML 1.2
# (they WERE in YAML 1.1 — common gotcha)
answer: yes # YAML 1.1: true; YAML 1.2: "yes" string
# Numbers with leading zeros
version: 010 # YAML 1.1: octal 8; YAML 1.2: string "010"
# Unquoted strings with special chars
url: example.com/path # OK
url: example.com:8080 # WRONG: parsed as mapping with key example.com
# Empty values
empty: # null
empty2: ~ # explicit null
empty3: "" # empty string (different from null)YAML vs JSON
YAML 1.2 was designed so JSON is a strict subset — any JSON file is valid YAML. YAML adds comments, block structure, anchors, multi-line strings and tags. JSON requires quoting all keys and string values, which YAML relaxes for plain scalars.
# JSON is a strict subset of YAML 1.2
# Every valid JSON document is valid YAML
# JSON:
{"name": "Alice", "age": 30, "hobbies": ["a", "b"]}
# Equivalent YAML:
name: Alice
age: 30
hobbies:
- a
- b
# YAML adds: comments, multi-line strings, anchors,
# relaxed quoting, tags, multiple documents
# YAML removes: mandatory quotes around strings,
# commas, braces (in block style)
# YAML is a superset of JSON — not the other way.
# Comments and anchors are not valid JSON.Scalars (Strings, Numbers, Booleans, Null)
Plain & Quoted Strings
Plain strings need no quotes unless they look like numbers, booleans, null, or dates. Single quotes only escape ' by doubling (''); no other escapes. Double quotes support full escape sequences (\n, \t, \uXXXX). Quoting forces string type.
# Plain strings (no quotes) — most common
name: Alice
path: /usr/local/bin
url: https://example.com
# Single-quoted — escape ' by doubling
msg: 'it''s fine'
# Double-quoted — supports escapes
greeting: "Hello\nWorld"
unicode: "\u03B1" # Greek alpha
tab: "col1\tcol2"
# Multi-word plain strings are fine
description: This is a plain string with spaces
# Strings that look like other types must be quoted
version: "1.0" # string, not float
notBool: "true" # string, not boolean
notNull: "null" # string, not null
date: "2024-01-01" # string, not dateNumbers
YAML 1.2 uses 0o for octal (YAML 1.1 used a bare leading 0). Underscores in numbers are allowed for readability. .inf and .nan are special float literals. Quote any value that should be a string but looks numeric (versions, phone numbers, IDs).
# Integers
int: 42
negative: -17
positive: +100
underscored: 1_000_000 # readable grouping (YAML 1.2)
# Octal & hex (YAML 1.2 prefixes)
octal: 0o17 # = 15 decimal
hex: 0xFF # = 255 decimal
binary: 0b1010 # = 10 decimal
# Floats
pi: 3.14159
exp: 1.0e+3 # 1000.0
neg_exp: -2.5e-4
# Special float values (IEEE 754)
infinity: .inf
neg_infinity: -.inf
not_a_number: .nan
# To force a string that looks numeric:
version: "1.0"
phone: "555-1234"Booleans
YAML 1.2 narrowed booleans to just true/false (and True/False, TRUE/FALSE). YAML 1.1 (used by PyYAML) treats yes/no/on/off/y/n as booleans — a famous source of bugs (e.g. Norway 'no' → false). Always quote these strings if you mean them literally.