NV ToolkitFormat · Validate · Convert
Tutorial

YAML Tutorial: Read and Write It Without Fear

YAML runs your deploys: Kubernetes, GitHub Actions, docker-compose, Ansible. It optimizes for human eyes — and punishes imprecision. This tutorial teaches the structure and the three pitfalls behind virtually every broken pipeline.

In short

YAML represents data with two structures — indentation-based mappings (key: value) and dash-prefixed lists — using spaces only, never tabs, for nesting. Most strings need no quotes, but three pitfalls break real pipelines: illegal tabs, silent indentation drift that reattaches a key to the wrong parent, and implicit typing (YAML 1.1's 'Norway problem', where NO becomes false). Anchors (&) and aliases (*) let you reuse blocks without repeating them.

By NaveenKumar T · Software engineer · builds and maintains NV Toolkit Published 2026-06-04 2 min read

Mappings, lists, and nesting

Two structures compose everything. Mappings: key: value, one per line (note the mandatory space after the colon). Lists: lines starting with a dash. Nesting is indentation — spaces only, tabs are illegal — and consistency matters more than the amount; two spaces is the universal convention.

yaml
service:
  name: checkout
  replicas: 3
  ports:
    - 8080
    - 8443
  env:
    - name: LOG_LEVEL
      value: info

Strings, quoting, and multi-line text

Most strings need no quotes — name: Ada Lovelace just works. Quote when the value could be misread: contains a colon-space, starts with a special character, or matches a keyword (more below). Multi-line text has two block styles: | preserves line breaks (literal), > folds them into spaces — invaluable for embedded scripts and certificates.

yaml
script: |
  #!/bin/sh
  echo "line breaks preserved"
  exit 0
summary: >
  These lines fold
  into one paragraph.

The three pitfalls that break pipelines

Tabs — invisible, illegal, and the first thing to suspect when a file 'looks right' but won't parse. Indentation drift — one extra space silently attaches a key to the wrong parent: the config loads fine and your option is just… ignored. Implicit typing — unquoted yes/no/on/off may become booleans (the infamous Norway problem: country: NO → false), version: 1.20 becomes the number 1.2, and 08:30 can surprise you. The cure for all typing surprises is one character: quotes.

Because two of the three failures are silent, validating before committing isn't paranoia — it's the workflow. Our YAML Validator reports the exact line and column, and the tree view shows which parent each key REALLY attached to.

PitfallWhat happensFix
TabsThe parser rejects the file — tabs are illegal for indentationUse spaces only, two per level
Indentation driftA key silently attaches to the wrong parentValidate and check the tree view
Implicit typingNO → false, 1.20 → 1.2, 08:30 → unexpected valuesQuote ambiguous scalars

Anchors: do-not-repeat-yourself YAML

An anchor (&name) labels a node; an alias (*name) reuses it; <<: merges it into a mapping. CI matrices and Kubernetes bases use this constantly. When converting YAML to JSON, anchors are resolved — every alias expands to a full copy, since JSON has no reference syntax.

yaml
defaults: &defaults
  retries: 3
  timeout: 30
production:
  <<: *defaults
  timeout: 60   # overrides the merged value
How this was tested: Syntax examples were validated with the site's own parser (yaml 2.9) on 2026-09-10.

Sources

  1. YAML Ain't Markup Language (YAML) Version 1.2.2
  2. YAML 1.1 — bool language-independent type
  3. GitHub Actions — Workflow syntax

Frequently asked questions

Why does YAML forbid tabs for indentation?

Because tab width is presentation, not data — your editor's 4-space tab is someone else's 8. Since indentation IS the structure in YAML, it must be unambiguous, so the spec allows only spaces. The error appears constantly because tabs are invisible.

What is the 'Norway problem'?

In YAML 1.1, unquoted no parses as boolean false — so country: NO (Norway's code) silently became country: false in countless configs. YAML 1.2 fixed it, but older parsers linger; quoting country codes, versions, and anything ambiguous remains the safe habit.

Do I need quotes around YAML strings?

Usually not — that's the point of YAML. Quote when the value could be misread as another type (yes, 1.20, null), contains a colon-space or starts with a special character. When in doubt, quotes never hurt; missing ones occasionally do.

Paste any YAML into the YAML Validator — full 1.2 parsing, exact error positions, and a tree view that exposes indentation mistakes instantly.

Validate your YAML

Keep working

Related tools