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.
Updated 28 October 2026
Format and validate YAML with line numbers, or convert between YAML and JSON.
Open the YAML formatterYAML 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]- Indent with spaces only. A tab in the indentation is an error.
- Put a space after the colon:
key: value.key:valueis one string. - Items at the same level must line up exactly. Two spaces per level is the usual style.
- YAML 1.2 is a superset of JSON, so any JSON document is also valid YAML.
Try ↗ buttons below open the example in the YAML formatter, which shows how it is parsed.
Values and how they are parsed
| Type | YAML | Parsed as | |
|---|---|---|---|
| Plain string | title: Hello world | "Hello world" | Try ↗ |
| Single-quoted | s: 'it''s' | "it's" — '' is a literal quote; no escapes | Try ↗ |
| Double-quoted | s: "tab\there" | Escapes like \n, \t and \u00e9 work | Try ↗ |
| Integer | n: 42 | 42 | Try ↗ |
| Float | n: 3.14 | 3.14 | Try ↗ |
| Hex / octal | n: 0x1F | 31. Octal in YAML 1.2 is 0o755 | Try ↗ |
| Infinity / NaN | n: .inf | Infinity (also -.inf and .nan) | Try ↗ |
| Boolean | ok: true | true / false | Try ↗ |
| Null | v: ~ | null — also null or an empty value | Try ↗ |
| Date | d: 2026-10-28 | A date in js-yaml and PyYAML; quote it to keep a string | Try ↗ |
Multi-line strings: | and >
| Style | YAML | Value |
|---|---|---|
| Literal — keeps line breaks | t: | line1 line2 | "line1\nline2\n" |
| Literal, no final newline | t: |- line1 line2 | "line1\nline2" |
| Folded — joins lines with spaces | t: > line1 line2 | "line1 line2\n" |
| Keep all trailing newlines | t: |+ | 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
| Problem | Example | What happens | Fix |
|---|---|---|---|
| The Norway problem | country: NO | YAML 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 strings | Quote it: country: "NO" |
| Version numbers | version: 1.10 | Parsed as the number 1.1 | Quote it: version: "1.10" |
| Leading zeros | mode: 0755 | Octal 493 in YAML 1.1, decimal 755 in YAML 1.2 | Quote it, or write 0o755 |
| Colon in a value | title: Note: read me | Error: a second key on the same line | Quote the value |
| Special first character | ref: *main | Strings starting with * & ! @ ` % | have special meanings | Quote the value |
| Tabs | A tab before key: | Error — tabs are not allowed for indentation | Indent with spaces |
| Duplicate keys | a: 1 … a: 2 | An error in js-yaml; other parsers silently keep the last one | Remove the duplicate |
| Dates | released: 2026-10-28 | Becomes a date object, not a string | Quote it if you need the text |
YAML in code
| Language | Read YAML | Write 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) |
| Ruby | YAML.safe_load(text) | data.to_yaml |
| Command line (yq) | yq '.spec.replicas' deploy.yaml | yq -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?
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?
yes, no, on, off and NO as booleans. Quote the value to keep it as text.Is JSON valid YAML?
What do --- and ... mean?
--- starts a new document, so one file can hold several. ... ends a document without starting a new one.