YAML Syntax Cheatsheet — Format Guide with Examples

YAML syntax reference: scalars, lists, maps, multiline strings, anchors, merge keys. For Docker, K8s, CI/CD configs.

Basic Syntax

# Key-value pairs
name: John Doe
age: 30
active: true

# Nested objects
address:
  street: 123 Main St
  city: Paris
  country: France

# Inline object
address: { street: 123 Main St, city: Paris }

Lists

# Block list
fruits:
  - apple
  - banana
  - cherry

# Inline list
fruits: [apple, banana, cherry]

# List of objects
users:
  - name: Alice
    role: admin
  - name: Bob
    role: user

Multiline Strings

# Literal block (preserves newlines)
description: |
  Line one.
  Line two.
  Line three.

# Folded block (joins lines with spaces)
description: >
  This is a long sentence
  that spans multiple lines
  but becomes one line.

# With chomp indicators
text: |+   # Keep trailing newlines
text: |-   # Strip trailing newlines

Data Types

string: "hello"
number: 42
float: 3.14
boolean: true       # or: false, yes, no, on, off
null_value: null    # or: ~
date: 2026-02-22
timestamp: 2026-02-22T10:30:00Z

Anchors & Aliases

# Define anchor
defaults: &defaults
  adapter: postgres
  host: localhost

# Use alias
development:
  <<: *defaults        # Merge defaults
  database: dev_db

production:
  <<: *defaults
  database: prod_db
  host: db.example.com

Environment Variables

# Docker Compose style
environment:
  - DB_HOST=${DB_HOST:-localhost}
  - DB_PORT=5432

Need These Tools as an API?

TextForge API offers 20+ developer toolkit endpoints. Free tier: 50 requests/day.

Try TextForge API Free →

Related Tools