If you’re already comfortable with YAML, you can skip this section. If not, this concise primer will help you read and write YAML for tools like Docker Compose, Kubernetes manifests, CI pipelines, and many configuration files.
Simple key-value pairs
At its simplest, YAML represents key-value pairs with the syntaxkey: value. Remember to include a space after the colon.
Lists (arrays)
Lists are built using a dash (-) under a key. Each dash indicates an element in the sequence.
Dictionaries (mappings)
Mappings (dictionaries) group related properties under a key. Indentation determines nesting; maintain consistent indentation (commonly two spaces per level). Do not mix tabs and spaces. Correct example:Fat and Carbs are incorrectly nested under Calories):
Indentation matters. Do not mix tabs and spaces. Choose a consistent number of spaces per nesting level (two is common) and stick to it to avoid parser errors.
Complex nesting: lists of dictionaries
YAML supports arbitrary nesting: lists can contain dictionaries, and dictionaries can contain lists. Here is a list where each element is a dictionary describing a fruit:Dictionary vs list — when to use which
Use mappings (dictionaries) to represent the properties of a single object. Use lists (arrays) to represent a sequence of objects of the same type.
Order: dictionaries vs lists
- Mappings (dictionaries) are unordered collections of key-value pairs. The order of keys typically does not matter:
- Lists (arrays) are ordered collections. The order of items matters:
Comments
Lines beginning with# are treated as comments and ignored by parsers:
Summary
YAML is a readable, widely used format for configuration. Remember:- Use
key: valuefor mappings. - Use
-for list items. - Maintain consistent indentation (no tabs).
- Choose mappings vs lists based on whether data is unordered key/value pairs or an ordered sequence.
- Use comments starting with
#.