yq/operators/collect-into-array.md
Chris Hamant 3c0fd6a3e9
docs: add hints for common syntax edge cases
Add informational hints to operator documentation covering behaviors
  that may differ from user expectations, particularly for those
  familiar with jq:

  - slice-array: clarify that slicing only works on arrays, not strings
  - select: document behavior when piping select to a literal value
  - equals: note bash history expansion with != operator
  - create-collect-into-object: mention that shorthand {key} syntax
    is not supported
  - collect-into-array: explain difference between [.x | .[]] and [.x[]]
2026-01-28 02:51:22 -05:00

706 B

Collect into Array

This creates an array using the expression between the square brackets.

{% hint style="warning" %}

Note the placement of | when collecting. These two forms behave differently:

# Pipe then splat - creates separate context
[.items | .[] | has("id")]

# Splat directly on path - more common pattern
[.items[] | has("id")]

{% endhint %}

Collect empty

Running

yq --null-input '[]'

will output

[]

Collect single

Running

yq --null-input '["cat"]'

will output

- cat

Collect many

Given a sample.yml file of:

a: cat
b: dog

then

yq '[.a, .b]' sample.yml

will output

- cat
- dog