Cheatsheet: YAML

Last updated 2026-06-21

Basic syntax

Scalars are simple values such as strings, numbers, booleans, and nulls.

name: John Doe
age: 30
is_active: true
middle_name: null

Lists are sequences represented by a dash followed by a space.

fruits:
  - apple
  - banana
  - cherry

Flow-style lists use JSON-like brackets.

fruits: [apple, banana, cherry]

Maps are key-value pairs separated by a colon and indented for nesting.

person:
  name: John Doe
  age: 30
  is_active: true

Flow-style maps use JSON-like braces.

person: {name: John Doe, age: 30, is_active: true}

Nested lists and maps are expressed with indentation, not braces.

services:
  web:
    image: nginx
    ports:
      - "8080:80"

Comments start with # and continue to the end of the line.

# This is a comment
name: John Doe # Inline comment

Strings and scalars

Plain strings usually do not need quotes, but quoting avoids ambiguity.

plain: hello world
single_quoted: 'keeps backslashes as text'
double_quoted: "supports escapes like 
"

Use single quotes by doubling them inside single-quoted strings.

message: 'It''s YAML'

Literal block scalars with | preserve line breaks.

script: |
  echo "one"
  echo "two"

Folded block scalars with > fold most line breaks into spaces.

summary: >
  This long sentence
  becomes one paragraph.

Chomping indicators control the final newline of block scalars.

keep: |+
  keep trailing newlines
strip: |-
  strip final newline

Explicit tags can force values to strings or other types.

version: !!str 1.0
count: !!int "42"

Reuse and documents

Anchors name a node and aliases reuse it elsewhere.

defaults: &defaults
  retries: 3
  timeout: 30
api: *defaults

Merge keys copy mappings from an anchored map.

defaults: &defaults
  image: node:20
  restart: unless-stopped
worker:
  <<: *defaults
  command: npm run worker

Multiple YAML documents in one stream are separated with ---.

---
name: first
---
name: second
...

Use explicit booleans and nulls for configuration values.

enabled: true
disabled: false
empty: null

Quote values that look like booleans, numbers, dates, or special characters when they must remain strings.

version: "1.0"
date: "2026-06-21"
yes_value: "yes"
colon_value: "host:port"

See also: