Short lists read better inline, and knowing JSON is valid YAML helps when tools mix the two.
Besides the indentation-based block style, YAML has a flow style borrowed from JSON:
- flow sequence:
[80, 443]; - flow mapping:
{cpu: 500m, memory: 1Gi}.
Flow collections can nest and span several lines, and inside them indentation doesn't matter. Use them for short, simple values; long or deeply nested data is easier to read in block style.
In flow context the characters ,, [, ], { and } are indicators, so a string containing them must be quoted inside a flow collection: [a, "b,c"].
YAML 1.2 was designed so that JSON is (nearly) a subset: a JSON document is a valid YAML document with the same meaning. That's why tools that accept YAML usually accept JSON files too, and why you can paste a JSON snippet into a YAML file as a value.
The reverse isn't true: YAML adds comments, anchors, multi-line strings, unquoted strings and non-string keys that JSON can't express. When converting YAML to JSON, those features are resolved or lost.
Block and flow styles produce identical data; the choice is purely presentation.
Example
# block style
ports:
- 80
- 443
resources:
cpu: 500m
memory: 1Gi
# the same data in flow style
ports2: [80, 443]
resources2: {cpu: 500m, memory: 1Gi}
# quote strings with flow indicators inside flow collections
tags: [web, "env,prod", "[beta]"]
# a JSON document pasted in as a value is valid YAML
raw: {"id": 7, "labels": ["a", "b"], "ok": true}Remember
- [a, b] and {k: v} are flow-style sequences and mappings
- Block and flow styles describe exactly the same data
- JSON is valid YAML 1.2; YAML adds comments, anchors and more
Watch out for
- Leaving an unquoted comma in a value inside [ ], which splits it into two items.