YAML Cheat Sheet

The syntax you need for config files, how each value is parsed, multi-line strings, anchors, and the gotchas that turn NO into false.

Cheat sheetPrintableFree, no login

Updated 28 October 2026

Format and validate YAML with line numbers, or convert between YAML and JSON.

Open the YAML formatter

YAML basics

# A comment
name: Alice            # key: value (a space after the colon)
age: 30
address:               # nested map: indent with spaces, never tabs
  city: London
  zip: "SW1A 1AA"
roles:                 # list
  - admin
  - editor
inline_map: {a: 1, b: 2}
inline_list: [x, y, z]

Try ↗ buttons below open the example in the YAML formatter, which shows how it is parsed.

Values and how they are parsed

TypeYAMLParsed as
Plain stringtitle: Hello world"Hello world"Try ↗
Single-quoteds: 'it''s'"it's" — '' is a literal quote; no escapesTry ↗
Double-quoteds: "tab\there"Escapes like \n, \t and \u00e9 workTry ↗
Integern: 4242Try ↗
Floatn: 3.143.14Try ↗
Hex / octaln: 0x1F31. Octal in YAML 1.2 is 0o755Try ↗
Infinity / NaNn: .infInfinity (also -.inf and .nan)Try ↗
Booleanok: truetrue / falseTry ↗
Nullv: ~null — also null or an empty valueTry ↗
Dated: 2026-10-28A date in js-yaml and PyYAML; quote it to keep a stringTry ↗

Multi-line strings: | and >

StyleYAMLValue
Literal — keeps line breakst: |
line1
line2
"line1\nline2\n"
Literal, no final newlinet: |-
line1
line2
"line1\nline2"
Folded — joins lines with spacest: >
line1
line2
"line1 line2\n"
Keep all trailing newlinest: |+Like | but trailing blank lines are kept

A blank line inside folded text (>) becomes a line break. Use | for scripts, certificates and anything where line breaks matter.

Anchors, aliases and merge keys

defaults: &defaults      # & names this node
  adapter: postgres
  pool: 5

development:
  <<: *defaults          # * reuses it; << merges its keys in
  database: dev_db

test:
  <<: *defaults
  pool: 2                # keys you set win over merged ones

<< merge keys come from YAML 1.1. js-yaml, PyYAML and most config tools support them, but they are not part of the YAML 1.2 core schema.

Multiple documents in one file

One file can hold several documents separated by ---. Kubernetes manifests use this to put several resources in one file. ... marks the end of a document and is rarely needed.

kind: Service
metadata:
  name: web
---
kind: Deployment
metadata:
  name: web

The YAML formatter handles multi-document files; YAML → JSON turns them into a JSON array.

YAML gotchas

ProblemExampleWhat happensFix
The Norway problemcountry: NOYAML 1.1 parsers such as PyYAML read NO, yes, on and off as booleans. YAML 1.2 parsers such as js-yaml keep them as stringsQuote it: country: "NO"
Version numbersversion: 1.10Parsed as the number 1.1Quote it: version: "1.10"
Leading zerosmode: 0755Octal 493 in YAML 1.1, decimal 755 in YAML 1.2Quote it, or write 0o755
Colon in a valuetitle: Note: read meError: a second key on the same lineQuote the value
Special first characterref: *mainStrings starting with * & ! @ ` % | have special meaningsQuote the value
TabsA tab before key:Error — tabs are not allowed for indentationIndent with spaces
Duplicate keysa: 1 … a: 2An error in js-yaml; other parsers silently keep the last oneRemove the duplicate
Datesreleased: 2026-10-28Becomes a date object, not a stringQuote it if you need the text

YAML in code

LanguageRead YAMLWrite YAML
Python (PyYAML)yaml.safe_load(text)yaml.safe_dump(data, sort_keys=False)
JavaScript (js-yaml)yaml.load(text)yaml.dump(data)
Go (yaml.v3)yaml.Unmarshal(data, &v)yaml.Marshal(v)
Java (SnakeYAML)new Yaml().load(text)new Yaml().dump(data)
RubyYAML.safe_load(text)data.to_yaml
Command line (yq)yq '.spec.replicas' deploy.yamlyq -o=json deploy.yaml (to JSON)

Use the safe loaders (safe_load, js-yaml's default load) for anything you didn't write yourself — full loaders can create arbitrary objects.

Frequently asked questions

Can I use tabs in YAML?
Not for indentation — YAML only allows spaces there, and a tab is a parse error. Tabs are fine inside quoted strings.
What is the difference between | and > in YAML?
| (literal) keeps every line break. > (folded) joins lines with spaces and keeps only blank-line breaks. Add - to drop the final newline: |-.
Why did my value turn into true or false?
Older YAML 1.1 parsers read words like yes, no, on, off and NO as booleans. Quote the value to keep it as text.
Is JSON valid YAML?
Yes, in YAML 1.2: every JSON document is a valid YAML document. The JSON → YAML converter turns JSON into the more readable block style.
What do --- and ... mean?
--- starts a new document, so one file can hold several. ... ends a document without starting a new one.
✓ Copied!