/cheatsheets/yaml/yaml-cheat-sheet
YAML cheat sheet: syntax, multiline strings and gotchas
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated 2026-10-20
- tool
- YAML
- versions
- YAML 1.1, YAML 1.2.2
- os
- any
YAML structures data with indentation (spaces only, never tabs), key: value pairs and - list items. The examples are parsed with PyYAML, which implements YAML 1.1, the version most tools still use.
The basics, parsed
cat > basics.yaml <<'EOF'
name: errcodex
ports: [80, 443]
servers:
- host: a.example.com
weight: 2
- host: b.example.com
weight: 1
description: |
Line one
Line two
summary: >
folded into
one line
EOF
python -c "import yaml, json; print(json.dumps(yaml.safe_load(open('basics.yaml')), indent=2))"Output
{
"name": "errcodex",
"ports": [
80,
443
],
"servers": [
{
"host": "a.example.com",
"weight": 2
},
{
"host": "b.example.com",
"weight": 1
}
],
"description": "Line one\nLine two\n",
"summary": "folded into one line\n"
}| keeps newlines, > folds them into spaces; both keep one trailing newline (|- and >- remove it).
YAML 1.1 surprises
python -c "
import yaml
print(yaml.safe_load('''
country: no
answer: on
version: 1.10
zip: 012345
quoted: '012345'
'''))"Output
{'country': False, 'answer': True, 'version': 1.1, 'zip': 5349, 'quoted': '012345'}In YAML 1.1, no, off, yes, on are booleans, 1.10 is the float 1.1, and a leading zero means octal. YAML 1.2 only treats true and false as booleans, but many parsers still default to 1.1. Quote values that must stay strings.
Syntax reference
| Syntax | Meaning |
|---|---|
| key: value | mapping entry (a space after the colon is required) |
| - item | list item |
| [a, b] / {k: v} | flow style list / mapping, same as JSON |
| "text\n" / 'text' | double quotes allow escapes, single quotes are literal |
| ~ or null | null |
| # comment | comment until the end of the line |
| &base / *base | anchor / alias (reuse a node) |
| <<: *base | merge keys from an anchored mapping (YAML 1.1 extension) |
| --- | start of a new document in the same file |
Sources
Related pages
compare · yamlJSON vs YAML vs TOML: which format for config and dataJSON is strict and universal but has no comments, YAML is readable but its typing surprises, TOML is explicit and made for config. One data set in all three.errors · jsonSyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSONJSON.parse fails with Unexpected token '<' when the response is an HTML page, usually an error or login page, not JSON. Check the status and content type first.config · httpStrict-Transport-Security (HSTS): max-age, includeSubDomains, preloadStrict-Transport-Security makes browsers use only HTTPS for max-age seconds. includeSubDomains covers subdomains; preload is a commitment that is hard to undo.