Say you're iterating over a list of objects in jq:
[
{"foo": "bar", "other": "attributes"},
{"foo": "baz", "should": "be filtered"}
]
and you are only interested in operating on the attribute foo
:
jq ".[].foo"
will not give you two distinct objects:
"bar"
"baz"
but not a new list. So whatever you do, the next pipeline will operate on each single object. For example jq ".[].foo | length"
is the length of each string:
3
3
To get everything back into an array, you have to wrap the first pipeline into []
in the correct place:
$ jq "[.[].foo] | length"
evaluates to
2
—
I know this might be obvious to most people but for me it was really hard to wrap [sic!] my head around. So I decided to publish a blog post as a cheat-sheet for myself. 😄