Basic key-value pairs
A YAML key-value pair uses a colon to separate the key and value. For readability and to avoid parsing issues, put a space after the colon.
-) before each item. Leave a space after the dash.
Fat and Carbs are indented incorrectly so they appear to be children of Calories — which is invalid because Calories already has a scalar value.
Incorrect (invalid)
Use a consistent indentation style (2 spaces per level is common). Avoid mixing tabs and spaces. Consistent indentation prevents unexpected nesting and parser errors.
Always put a space after the colon (
Key: Value) and after the dash in list items (- item). Omitting these spaces can cause subtle parsing errors in some YAML parsers.Fruitsis a key whose value is a list.- Each list element (
- Banana:and- Grape:) is a mapping with its own nested keys (Calories,Fat,Carbs).
Best practices and tips
- Use consistent indentation (2 spaces per level recommended).
- Always use a space after colons and dashes.
- Prefer explicit quoting for values that could be interpreted (e.g.,
yes,no,on,off, numeric strings). - Validate YAML with a linter or parser before applying it to production systems (CI checks are helpful).
- When authoring large files, split logical sections into separate files and reference them where supported (e.g., Kubernetes manifests with kustomize or Helm values).
- YAML is a concise, human-readable format for structured data and configuration.
- Use
key: valuepairs,-for lists, and indentation to create nested structures. - Be consistent with spacing and indentation to avoid parser errors.