Basic key-value pair
A YAML mapping (dictionary) stores key-value pairs. Keys and values are separated by a colon followed by a space. Example:Arrays / lists
A YAML sequence (array/list) uses a dash and a space for each element. Example:Dictionaries (mappings)
A mapping groups named properties under a key. Here are nutrition details for two fruits represented as mappings:Indentation matters
YAML uses indentation (spaces) to represent structure. Indentation must be consistent: items at the same level use the same number of spaces. Mixing tabs and spaces or using inconsistent indentation can change structure or cause parser errors. For example, these properties belong toBanana because they share the same indentation level:
Fat and Carbs more than Calories, YAML will treat those values as nested under Calories, which is usually invalid and causes parser errors.
Always use consistent spaces for indentation (do not mix tabs and spaces). Two spaces per level is common, but the important part is consistent indentation across the file.
Nested structures: lists of mappings and mappings in lists
YAML supports arbitrarily nested structures. You can combine sequences and mappings to represent complex objects. For example, a list of fruits where each element is a mapping containing nutrition properties:When to use a mapping vs a list
Use mappings (dictionaries) to represent a single object with named properties. Use sequences (lists) when representing multiple items of the same type. Example — a single car as a mapping:List of dictionaries (full example)
A longer sequence of object records (list of mappings):Key notes: mappings vs sequences
- Mappings are unordered collections of key-value pairs; key order is not significant for equivalence.
- Sequences are ordered; changing the order changes the sequence:
Comments
Any text after a hash (#) on a line is treated as a comment and ignored by YAML parsers.
Example:
Incorrect indentation or trying to use the same key for both a scalar value and a mapping will produce parser errors. Keep scalars and mappings separate, and maintain consistent indentation to avoid “mapping values are not allowed here” and similar errors.
That covers the YAML basics you need for the upcoming lessons. Try practicing these concepts with hands-on exercises and real configuration files (for example: Docker Compose, Kubernetes manifests, CI config files). Practicing will help you avoid common pitfalls like indentation mistakes and improper mixing of lists and mappings.
Links and References
- YAML official site and specification: https://yaml.org/
- YAML Tutorial (learn practical syntax): https://www.yaml.org/start.html
- Kubernetes YAML basics: https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects/
- Examples of YAML in popular tools:
- Docker Compose: https://docs.docker.com/compose/compose-file/
- GitHub Actions workflow syntax: https://docs.github.com/actions/learn-github-actions/workflow-syntax-for-github-actions