Arrays and Lists in JSON vs YAML

Learn how JSON arrays map to YAML lists using dash syntax. Covers simple arrays, arrays of objects, nested arrays, and mixed-type list formatting.

Structure

Detailed Explanation

Arrays are one of the areas where JSON and YAML syntax differ most visibly. JSON uses square brackets and commas, while YAML uses dashes and indentation to represent ordered lists.

Simple array in JSON:

{
  "fruits": ["apple", "banana", "cherry"],
  "ports": [80, 443, 8080]
}

The same data in YAML:

fruits:
  - apple
  - banana
  - cherry
ports:
  - 80
  - 443
  - 8080

Arrays of objects in JSON:

{
  "users": [
    {"name": "Alice", "role": "admin"},
    {"name": "Bob", "role": "editor"}
  ]
}

In YAML (block style):

users:
  - name: Alice
    role: admin
  - name: Bob
    role: editor

Each - starts a new array element. Properties of the same object are indented at the same level below the dash.

Nested arrays:

{ "matrix": [[1, 2], [3, 4], [5, 6]] }
matrix:
  - - 1
    - 2
  - - 3
    - 4
  - - 5
    - 6

YAML also supports inline (flow) style for short arrays: fruits: [apple, banana, cherry]. This is identical to JSON syntax and useful when arrays are short and readability isn't compromised.

Important: In YAML, the dash-space (- ) prefix is part of the indentation. A common mistake is misaligning array items, which changes the structure entirely.

Use Case

Converting a list of API endpoints or route definitions from a JSON configuration file into YAML format for a framework like Swagger/OpenAPI that commonly uses YAML.

Try It — JSON ↔ YAML Converter

Open full tool