Chapters: JavaScript HTML & CSS JSON XML YAML ← Full TOC

← YAML Index

📝 YAML Syntax — Scalars, sequences, mappings, anchors, and multiline strings

YAML (YAML Ain't Markup Language) is a human-friendly data serialization format. It is a superset of JSON — all valid JSON is valid YAML. YAML uses indentation for structure (no braces or brackets needed). Used widely in Kubernetes, Ansible, Docker Compose, GitHub Actions, and CI/CD pipelines.

📋 Reference

ItemSyntax / ValueDescription
------ (document start marker)Start of YAML document — required for multi-doc files
...... (document end marker)End of YAML document
# comment# This is a commentHash starts a comment — inline or full line
stringname: Alice SmithUnquoted string — most common
quoted stringname: 'Alice Smith' or "Alice"Single quotes: literal. Double: escape sequences
integerage: 30Decimal integer
floatgpa: 3.95Floating point number
scientificdistance: 1.5e10Scientific notation
booleanactive: true / falsetrue/false (also yes/no, on/off in YAML 1.1)
nullnotes: null / notes: ~Null value
datecreated: 2026-06-25ISO 8601 date — auto-parsed as date object
datetimeupdated: 2026-06-25T12:00:00ZISO 8601 datetime
mapping (object)name: Alice age: 30Key-value pairs — the YAML object
nested mappingaddress: city: LA state: CAIndented nesting
sequence (array)- Python - Go - RustBlock sequence with -
inline sequencelangs: [Python, Go, Rust]Flow sequence — JSON-style
inline mappingaddr: {city: LA, state: CA}Flow mapping — JSON-style
multiline literal |notes: | Line 1 Line 2Preserve newlines exactly
multiline folded >desc: > Long line continuesFold newlines into spaces
anchor &defaults: &def timeout: 30Define reusable anchor
alias *prod: <<: *defReference anchor (merge key)
merge key <<<<: *defaultsMerge all keys from anchor
explicit type!!str 42Force value type
multi-document--- doc1 --- doc2Multiple documents in one file
key with spaces'my key': valueQuote keys with special chars
key with colon'url: value': textMust quote keys containing colon-space

💡 Example

---
# YAML Complete Syntax Reference
# MyWebUniversity.com — Chapter 34

# ── Scalars ──────────────────────────────────────────────────
name:        Alice Smith          # unquoted string
title:       "Senior Developer"   # double-quoted (allows \n etc.)
description: 'It''s great'        # single-quoted ('' = literal ')
age:         30                   # integer
gpa:         3.95                 # float
salary:      95_000               # underscore separator (readable)
active:      true                 # boolean
retired:     false
notes:       null                 # null
alias_null:  ~                    # null (alternative)
created:     2026-06-25           # date
updated:     2026-06-25T08:00:00Z # datetime (UTC)
hex:         0xFF                 # hexadecimal integer
octal:       0o77                 # octal
binary:      0b1010               # binary
scientific:  1.5e10               # scientific notation

# ── Multiline strings ─────────────────────────────────────────
bio: |
  Alice is a senior developer at MyWebUniversity.
  She specializes in Python and Go.
  Newlines are preserved exactly.

summary: >
  This text spans multiple lines
  but newlines are folded into spaces.
  Use for long prose paragraphs.

trimmed: |-
  No trailing newline (- modifier)

# ── Sequences (arrays) ───────────────────────────────────────
languages:
  - Python
  - Go
  - JavaScript
  - Rust

inline_langs: [Python, Go, Rust]   # flow style

nested_list:
  - name: Alice
    score: 95
  - name: Bob
    score: 87

# ── Mappings (objects) ───────────────────────────────────────
address:
  street: "123 Learn Ave"
  city:   Techville
  state:  CA
  zip:    "90210"               # quote to prevent int parsing
  country: US

inline_addr: {city: LA, state: CA}  # flow style

# ── Anchors and aliases ──────────────────────────────────────
defaults: &defaults
  timeout:  30
  retries:  3
  debug:    false
  log_level: info

development:
  <<: *defaults               # merge all keys from defaults
  debug: true                 # override specific key
  log_level: debug

production:
  <<: *defaults
  timeout:  60
  replicas: 3

# ── Nested complex structure ──────────────────────────────────
courses:
  - id:       CS101
    title:    Introduction to Python
    credits:  3
    grade:    A
    tags:     [beginner, python, free]
    metadata:
      created:   2024-01-15
      updated:   2026-06-01
      published: true

  - id:       CS201
    title:    "JavaScript & Node.js"   # & requires quoting
    credits:  3
    grade:    A-
    tags:     [intermediate, javascript, nodejs]
    metadata:
      created: 2024-06-01
      updated: 2026-06-15
      published: true

🏠 Index  |  YAML in Python →