🌐 English

Home › Guides

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.

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.

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.

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.

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.