mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-03 10:55:36 +00:00
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[]]
55 lines
706 B
Markdown
55 lines
706 B
Markdown
# 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:
|
|
|
|
```bash
|
|
# Pipe then splat - creates separate context
|
|
[.items | .[] | has("id")]
|
|
|
|
# Splat directly on path - more common pattern
|
|
[.items[] | has("id")]
|
|
```
|
|
|
|
{% endhint %}
|
|
|
|
## Collect empty
|
|
Running
|
|
```bash
|
|
yq --null-input '[]'
|
|
```
|
|
will output
|
|
```yaml
|
|
[]
|
|
```
|
|
|
|
## Collect single
|
|
Running
|
|
```bash
|
|
yq --null-input '["cat"]'
|
|
```
|
|
will output
|
|
```yaml
|
|
- cat
|
|
```
|
|
|
|
## Collect many
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
a: cat
|
|
b: dog
|
|
```
|
|
then
|
|
```bash
|
|
yq '[.a, .b]' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
- cat
|
|
- dog
|
|
```
|
|
|