Is it possible to filter an entire array of items in JQ in only one pass? Compare the following code, which runs jq over and over:
{
"foofoo": {
"barbar": [
{
"foo": "aaaa",
"bar": 0000
},
{
"foo": "bbbb",
"bar": 1111
},
{
"foo": "cccc",
"bar": 2222
}
]
}
}
bash array:
array=("1111" "2222")
my code is working but not very efficient and uses a lot of resources considering the array size in reality:
for k in "${array[@]}"; do
jq --argjson k "$k" '.foofoo.barbar |= map(select(.bar != $k))' json.json | sponge json.json
done
It keeps looping through the array, removing the unneeded entries and storing same file again by using sponge.
any ideas how to achieve a similar behavior with a lighter code?
Desired output:
{
"foofoo": {
"barbar": [
{
"foo": "aaaa",
"bar": 0
}
]
}
}
0000as output, it will be collapsed to"bar": 00or0000will not affect the functionality