YAML Flow Style

YAML offers two ways to write the same data. Block style uses line breaks and indentation. Flow style uses brackets, braces, and commas, similar to JSON. Both describe identical structures, and knowing flow style helps you read compact files and mix formats wisely.

Block Style Recap

Block style spreads data across lines. Most YAML files use it because it reads like an outline.

person:
  name: Asha
  skills:
    - python
    - sql

Flow Lists

Square brackets hold a list on one line. Commas separate the items.

skills: [python, sql, docker]
primes: [2, 3, 5, 7]
empty: []

Compact lists work well for short collections such as tags, ports, and coordinates.

Flow Mappings

Curly braces hold a mapping on one line. Each pair uses a colon and a space, and commas separate pairs.

point: {x: 4, y: 9}
user: {name: Ravi, role: admin}
empty: {}

Side-by-Side Conversion

 BLOCK STYLE                 FLOW STYLE
 -----------                 ----------
 car:                        car: {make: Tata, seats: 5}
   make: Tata
   seats: 5

 colors:                     colors: [red, green, blue]
   - red
   - green
   - blue

The pairs of examples carry the same data. A program reading either form receives the same dictionary and the same list.

Nesting Flow Collections

Flow collections nest inside each other:

students: [{name: Meena, marks: 91}, {name: Ravi, marks: 82}]
matrix: [[1, 2], [3, 4]]

Short nested structures stay readable in this form. Longer ones become hard to scan, and block style serves better.

Mixing Styles

A block mapping can contain flow values, which gives a good balance between structure and brevity:

server:
  name: web1
  ports: [80, 443]
  limits: {cpu: 2, memory: 512}

The outer structure uses indentation. The inner details fit on one line each.

YAML and JSON

Valid JSON is almost always valid YAML, because flow style mirrors JSON syntax. A JSON document can be pasted into a YAML file and loaded without changes in most cases.

{"name": "Asha", "age": 25, "tags": ["a", "b"]}

That fact helps teams migrate between formats. Configuration written in JSON can move into YAML gradually.

Rules to Remember

RuleExample
Commas separate items[1, 2, 3]
Colon needs a space after it{a: 1}
Strings with commas need quotes["a, b", c]
Trailing commas are allowed[1, 2, 3,]
Comments are allowed[1, 2] # note

Multi-Line Flow Collections

Flow collections can span several lines. Indent the continuation lines deeper than the parent key:

servers: [
  alpha,
  beta,
  gamma
]

This layout resembles JSON formatting and helps when a list grows long.

When to Choose Each Style

  • Block style suits configuration that people edit often.
  • Short lists and small coordinates fit flow style.
  • Flow style helps when copying data from JSON sources.
  • Avoid deep flow nesting, because commas and brackets hide the structure.

Strings Inside Flow Collections

Flow style gives commas, brackets, and braces special meaning. A text value containing any of those characters needs quotes, otherwise the parser splits it in the wrong place.

places: ["Delhi, India", "Pune, India"]
note: {text: "Use {braces} carefully"}

The quoted entries stay whole. Without quotes, the first list would contain four items instead of two.

Common Flow Style Mistakes

  • Forgetting the space after a colon, as in {a:1}, which creates a single text key.
  • Leaving a bracket or brace unclosed at the end of a long line.
  • Placing a comma inside a value without quoting it.
  • Cramming deep nesting onto one line until nobody can read it.

Syntax errors in flow collections often point to the end of the file, because the parser keeps reading while it waits for the missing closing symbol. Search backward for the opening bracket that has no partner.

Readability decides the question. If a line stretches beyond the screen width, switch it to block style.