YAML Gotchas: The Norway Problem, Octal Numbers and Tabs
YAML is easy to read and surprisingly easy to get wrong. The trouble is that a plain, unquoted value is guessed to be a string, number, boolean or null, and the guess depends on which YAML version the parser follows. Here are the surprises that cause most bugs, and how to check your file.
The Norway problem
In YAML 1.1, the words yes, no, on, off, y and n can all mean a boolean. A list of country codes then goes wrong:
countries:
- SE
- NO
- DK
A parser that follows YAML 1.1 reads NO as false, so Norway disappears. The fix is to quote it: "NO". YAML 1.2 narrowed booleans to only true and false, but many libraries still follow 1.1 rules, so quoting is the safe habit for any value that could be misread.
Numbers that change
- Leading zero. In YAML 1.1,
010is octal and means 8. In 1.2 it is 10 or stays a string depending on the schema. Quote zip codes, phone numbers and codes. - Version numbers.
version: 1.10is the number 1.1, not the text 1.10. Write"1.10". - Underscores and colons. Some parsers read
1_000as 1000, and 1.1 even read12:30as a base 60 number. Quote anything that is really an identifier. - Very large integers. Many tools turn them into floating point and lose digits. A converter that keeps exact integers avoids this.
Empty values and special words
key: with nothing after it is null, not an empty string. Use key: "" for an empty string. The word null and a single tilde (~) are also null.
Indentation and tabs
YAML uses spaces to show structure, and tab characters are not allowed for indentation. A tab pasted from another editor is a common cause of the error that says it found a character that cannot start a token. Keep the same number of spaces for items at the same level. A list item under a key can be indented or not, but be consistent.
Colons and special characters
A value that contains : (colon and space) or starts with a special character such as *, &, !, % or @ must be quoted. Without quotes, message: Error: disk full is a parse error.
Anchors and merge keys
Anchors (&name) and aliases (*name) let you reuse a block, and <<: *name merges it into another. They are common in CI and Docker Compose files. They disappear when you convert to JSON, because JSON has no references, so the result repeats the values.
Check what your file really means
The quickest test is to convert it and look. Paste the file into the YAML to JSON converter: it follows YAML 1.2 rules, resolves anchors, keeps large integers exact, and points to the line and column of a syntax error. If a value appears as a string where you expected a boolean, or the reverse, you have found the ambiguity. To go the other way, use the JSON to YAML converter, which quotes values that would otherwise be misread.