Skip to content

Menu

YAML Cheat Sheet: Quoting, Block Scalars, Anchors and the Norway Problem

Checked against YAML YAML 1.2.2 spec; run on yaml 2.9.1 (npm), PyYAML 6.0.4 and ruamel.yaml 0.19.1 on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for anyone who edits YAML by hand: Kubernetes manifests, CI pipelines, Compose files, app config. It follows the YAML 1.2.2 specification (revision dated 2021-10-01, no normative changes from 1.2) and every example was parsed with the yaml npm package 2.9.1, with PyYAML 6.0.4 and ruamel.yaml 0.19.1 used to show where YAML 1.1 parsers disagree.

When to use what

Leave strings unquoted only when they are obviously text. The moment a value could look like a number, a boolean, a null, a date or contains : or #, quote it. Use single quotes by default because nothing inside them is an escape except a doubled ''. Switch to double quotes only when you need an escape such as \n or \t.

For multi-line text, | keeps your line breaks and suits scripts, certificates and SQL. > folds single newlines into spaces and suits long prose such as descriptions. Then pick the chomping indicator: - drops the final newline, nothing keeps exactly one, + keeps every trailing blank line. Shell scripts in CI steps usually want plain |.

Flow style ([a, b], {k: v}) is fine for short lists on one line. Anything longer reads better in block style, and diffs are cleaner one item per line.

The parser matters as much as the file. Libraries that follow 1.2's core schema treat yes, on and NO as strings; YAML 1.1 parsers such as PyYAML turn them into booleans. If you can't control which parser reads your file, quote those values. Paste a file into the YAML validator to catch tabs and duplicate keys, or see the parsed result in the YAML to JSON converter. Our guide to common YAML mistakes and the YAML vs JSON post go further.

Results below are from the yaml npm package 2.9.1 with its default YAML 1.2 core schema.

name: plain text

Plain scalar: no quotes, read as a string unless it matches a number, bool or null pattern

a: 'it''s'

Single quotes: the only escape is a doubled quote, so this parses to it's

b: "tab\there"

Double quotes: backslash escapes such as \t, \n and \u00e9 are processed

c: 'no \t escape'

Inside single quotes a backslash is a literal character, so \t stays two characters

d: plain # comment

A # after whitespace starts a comment; the value is just plain

e: "#not comment"

Quote a value that starts with #, or it becomes an empty value plus a comment

msg: a: b

Fails: a plain value can't contain : . Parser error is about nested mappings in compact mappings; quote it

v: 1.10 / ver: "1.10"

Unquoted 1.10 is the float 1.1, so version strings lose their trailing zero; quote them

The 1.2 core schema (spec section 10.3) lists exactly which plain values resolve to non-strings. Anything else is a string.

~ | null | Null | NULL | (empty)

All resolve to null; key: with nothing after it is null, not an empty string

true | True | TRUE | false | False | FALSE

The only booleans in YAML 1.2; yes, on, n and NO stay strings

0o14 | 0x1F | 014

Octal needs 0o in 1.2 (0o14 is 12); 014 is decimal 14, but YAML 1.1 parsers read it as octal 12

.inf | -.Inf | .nan

Special floats; in JavaScript they come back as Infinity, -Infinity and NaN

1e3

Scientific notation resolves to the number 1000

!!str 123 / !!str yes

Explicit tag forces a string without quotes

Chomping only affects the line breaks at the very end of the block. Indentation inside the block is relative to its first line unless you add an indentation indicator such as |2.

k: |

Literal block, default clip chomping: keeps inner newlines plus one final newline (line1\nline2\n)

k: |-

Literal, strip: no trailing newline (line1\nline2). Good for tokens and single-line secrets

k: |+

Literal, keep: every trailing blank line is preserved (line1\nline2\n\n\n)

k: >

Folded: single newlines become spaces, a blank line becomes \n (one two\npara\n)

k: > (with a more-indented line)

Lines indented further than the block are not folded, so one\n indented\ntwo\n keeps its breaks

- a - b

Block sequence: one item per line, - then the value

list: [a, b, {k: v}]

Flow sequence, can hold flow mappings; parsed as ["a","b",{"k":"v"}]

map: {x: 1, y: [2, 3]}

Flow mapping; handy for short inline objects

a: 1 a: 2

Duplicate key: the spec requires unique keys. yaml 2.9.1 and ruamel.yaml error; PyYAML 6.0.4 silently keeps the last value

<TAB>b: 1

Tabs are not allowed for indentation; the error reads Tabs are not allowed as indentation

Anchors and aliases are core YAML. The << merge key is a YAML 1.1 type (yaml.org/type/merge.html) that the 1.2 spec does not include, so support depends on the library. The yaml npm docs at eemeli.org/yaml say its default depends on the YAML version.

base: &b {x: 1, y: 2}

&b names this node so it can be reused

ref: *b

*b is an alias: a copy of the anchored node ({x: 1, y: 2})

child: <<: *b y: 9

Merge key: copies keys from *b, local keys win, giving {x: 1, y: 9}

<<: [*a, *b]

Merge several maps; for duplicate keys the earlier map in the list wins ({x: 1, z: 3} for a={x:1}, b={x:2,z:3})

YAML.parse(src, { merge: true })

In yaml 2.9.1 with default options << stayed a literal key; passing merge: true applied it

---

Starts a document; one file can hold several (Kubernetes uses this for many objects per file)

...

Ends a document without starting a new one

%YAML 1.2

Directive naming the spec version; it goes before ---

YAML.parseAllDocuments(src)

yaml npm: read every document; YAML.parse throws Source contains multiple documents

yaml.safe_load_all(stream)

PyYAML: iterate documents safely; never call yaml.load on untrusted input

From the docs, not run by us
country: NO

Norway problem: "NO" in yaml 2.9.1 and ruamel.yaml, false in PyYAML 6.0.4 (YAML 1.1 rules). Quote it

port: 22:22

YAML 1.1 sexagesimal: PyYAML reads 1342; 1.2 parsers keep the string. Quote port maps

Worked example: Find out why a CI config behaves differently in two tools

A config file is read by a Node service and by a Python script. The Python side keeps disabling a feature. Here is the file:

feature_flags:
  beta: on
  country: NO
ports:
  - 22:22
script: |-
  echo build
  echo test

I ran it through both parsers:

yaml 2.9.1 (Node)  -> {"beta": "on", "country": "NO", "ports": ["22:22"]}
PyYAML 6.0.4       -> {"beta": true, "country": false, "ports": [1342]}

The Node library follows the YAML 1.2 core schema, so on, NO and 22:22 are plain strings. PyYAML uses YAML 1.1 rules: on becomes true, NO becomes false, and 22:22 is read as base-60 and becomes 1342. The script block is the same in both: |- keeps the inner newline and drops the trailing one, so the value is echo build\necho test.

The fix is to quote the ambiguous values and write booleans the 1.2 way:

feature_flags:
  beta: true
  country: "NO"
ports:
  - "22:22"

Now both parsers produce identical output. When a file is shared across languages, test it in each one; a passing check in one parser proves nothing about the other.

Common mistakes

  • Writing yes, no, on or off and expecting a boolean everywhere.

    Use true and false. Under YAML 1.2 the other words are strings, and under 1.1 parsers they are booleans, so they mean different things in different tools.

  • Indenting with tabs because the editor inserted them.

    Indent with spaces only. Set your editor to insert spaces for .yml and .yaml files; the parser stops with a tab error otherwise.

  • Leaving version numbers and ZIP codes unquoted, e.g. version: 1.10 or zip: 01234.

    Quote anything that must keep its exact characters. 1.10 becomes 1.1, and a leading zero can turn into an octal number in 1.1 parsers.

  • Using | for a token or password and then getting a trailing newline in the header or file.

    Use |- to strip the final newline, or put the value on one line in quotes.

  • Relying on << merge keys in a file that a strict YAML 1.2 tool will read.

    Check that every consumer supports merge keys (the yaml npm package needs merge: true in 1.2 mode). Otherwise repeat the keys or use anchors on whole values only.

  • Repeating a key further down a long file and assuming the parser will complain.

    PyYAML keeps the last value without a warning. Run the file through a linter or a parser that rejects duplicates before you trust it.