BudiBadu Logo
Samplebadu

YAML by Example: Block vs Flow Styles

1.2

Two ways to write the same data. Choose based on readability needs.

Code

# Block style for lists (readable)
fruits:
  - Apple
  - Banana
  - Orange

# Flow style for lists (compact)
colors: [red, green, blue]

# Block style for maps
person:
  name: Alice
  age: 25

# Flow style for maps
point: { x: 10, y: 20 }

# You can mix styles
users:
  - { name: Bob, id: 1 }
  - { name: Carol, id: 2 }

Explanation

Block style uses indentation and line breaks to show structure, making it easy to read for complex data. Flow style uses brackets and braces, similar to JSON, for a more compact representation. Block style is preferred for configuration files while flow style works well for simple inline data.

You can freely mix both styles in the same file. For example, use block style for your main structure and flow style for simple coordinate pairs or small objects. The parser treats them identically, so choose based on what makes your data easiest to understand.

Code Breakdown

2-5
Block style list uses dashes and new lines.
8
Flow style list uses square brackets like JSON arrays.