YAML pitfalls: indentation, quotes, and yes/no values
YAML is a pleasant format for settings, but it has many invisible rules, so the data is often read differently from what you meant. Indentation, quoting, yes and no values, and automatic type conversion cause most surprises. Knowing them in advance makes configuration errors far easier to track down.
Indentation is the structure
Instead of brackets, YAML shows hierarchy with indentation. Items at the same depth must use the same number of spaces, and being off by one space can attach an item to a different parent or cause an error. Teams usually settle on two or four spaces.
Tab characters cannot be used for indentation. An editor that inserts tabs may look aligned yet produce an unreadable file, so configure your editor to turn tabs into spaces.
A list item is written - value and needs a space after the dash. -value is just text.
A key needs a space after the colon. name:Kim is read as a single piece of text.
After pasting, convert the file to JSON to check that mixed indentation has not changed the structure.
Values that need quotes
YAML allows unquoted strings, but special characters change the meaning. A value containing : (colon and space), or a # preceded by a space, is treated as a mapping or a comment and the value gets cut. Wrap such values in quotes.
Double-quoted strings interpret escapes like \n, while single-quoted strings keep text literally. To write a single quote inside single quotes, double it.
title: Re: notice can fail, so write title: "Re: notice".
Values starting with *, &, !, @ or a backtick also need quotes.
When you wonder whether quotes are needed, adding them is always the safe choice.
Yes/no and automatic number conversion
The best-known trap is that yes, no, on and off can be read as booleans depending on the format version. A country code NO for Norway written without quotes may become false. Write booleans only as true and false, and quote anything that is meant as text.
Numbers behave the same way. 0123 may be read as octal, 1e3 as an exponent, and 1.10 as 1.1, which changes a version number. Postal codes, phone numbers and versions should be written as strings.
version: 1.10 can be read as 1.1, so write "1.10".
null, ~ and an empty value are all read as nothing.
Something date-like such as 2024-01-05 may become a date value, so quote it if you want text.
Multi-line text and comments
Long text can be written with | (keep line breaks) or > (fold line breaks into spaces). A trailing - removes the last line break, and because indentation defines the block, the first line inside it must keep a consistent indent.
YAML supports comments that start with #. When you convert to JSON the comments disappear, so if important information lives only in comments, check before converting.
| is convenient for scripts or certificates.
> suits a long sentence that should end up on one line.
Converting JSON to YAML cannot bring comments back, so keep the original file separately.
A habit of checking
YAML is easy for people to read, which also makes it easy to miss when the reading differs from your intent. The most reliable check is to convert the file to JSON. The structure becomes visible through brackets, and you immediately see whether a value is a string, a number or a boolean.
The JSON to YAML converter on this site lets you paste a configuration and look at the converted result. Checking once more before deployment greatly reduces incidents.
In the converted output, check whether unquoted values came out in the form you intended by looking for quotes.
Duplicate keys may overwrite each other or fail, so make sure the same key does not appear twice.
Agree on a team style, for example always quoting strings, and mistakes drop.