Skip to content
Block Mappings
step 1/5

Reading — step 1 of 5

Read

~1 min readBlock Style

Block Mappings

A block mapping is the standard key: value per-line form.

name: Alice
age: 30
active: true

Implicit type inference applies to value:

  • 30 → integer.
  • true → boolean.

Force string with quotes.

Nested:

person:
  name: Alice
  contact:
    email: [email protected]
    phone: "+1-555-1234"

Empty value:

field:           # null
field:           # also null

Multiple keys on one line: NOT allowed in block style. Use flow style.

Comments:

# Top-level comment
name: Alice  # inline comment
age: 30
# Comment between fields
active: true

Comments are NOT part of the data. Most parsers drop them. Some preserve via "round-trip" parsers (ruamel.yaml).

Multi-document:

---
name: doc1
---
name: doc2
...

--- separates documents. ... ends one (rarely used).

Anchors and aliases:

defaults: &defaults
  timeout: 30
  retries: 3

production:
  <<: *defaults
  hostname: prod.example.com

staging:
  <<: *defaults
  hostname: staging.example.com

&defaults: anchor (define). *defaults: alias (reference). <<: merge key (merge map into current).

Result: production has timeout, retries, hostname. Staging same.

Common in big k8s/Helm configs to reduce duplication.

Some parsers reject merge keys (it's an extension, not core spec).

Naming convention: kebab-case or snake_case for keys (Helm, k8s favor camelCase via Go).

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…