Reputation: 47
I am trying to use jq to output a single boolean it a element exists in array:
jq -e '.differences[] | .afterBlob.path | contains ("sandbox_app") or contains ("sandbox_cicd")'
Input:
{
"differences": [
{
"beforeBlob": {
"blobId": "a9033f024bf",
"path": "test1",
"mode": "100644"
},
"afterBlob": {
"blobId": "e27f2609943e",
"path": "test1",
"mode": "100644"
},
"changeType": "M"
},
{
"beforeBlob": {
"blobId": "ec669676314",
"path": "test2",
"mode": "100644"
},
"afterBlob": {
"blobId": "38867b90873",
"path": "test2",
"mode": "100644"
},
"changeType": "M"
},
{
"afterBlob": {
"blobId": "ae8c5bdb690",
"path": "sandbox_app/test3",
"mode": "100644"
},
"changeType": "A"
},
{
"afterBlob": {
"blobId": "97819f382ad9",
"path": "sandbox_cicd/test3",
"mode": "100644"
},
"changeType": "A"
}
]
}
Current Output:
false
false
true
true
How can i get the output to just be a single boolean.
I have tried using the 'any' function:
jq -e '.differences[] | .afterBlob.path | contains ("sandbox_app") or contains ("sandbox_cicd") | any'
but get the following:
jq: error (at <stdin>:46): Cannot iterate over boolean (false)
I have also tried wrapping the 'any' function around my statement. Is there a way for jq to just output a single boolean if value exists
Upvotes: 2
Views: 1782
Reputation: 117027
any/2
allows for a considerably more efficient solution here than any/0
:
any(.differences[];
.afterBlob.path | contains ("sandbox_app") or contains ("sandbox_cicd") )
Upvotes: 2
Reputation: 532418
You need to wrap the current expression in an array, which can then be fed to any
:
jq -e '[.differences[].afterBlob.path |
contains ("sandbox_app") or contains ("sandbox_cicd")] |
any' foo.json
You can also write this using map
:
jq -e '.differences |
map(.afterBlob.path | contains ("sandbox_app") or contains ("sandbox_cicd")) |
any' foo.json
which is slightly longer, but you might find it more intuitive.
Upvotes: 3