Let's say I have json returned as such:
export json_string='{
"summary": "1 failure",
"checks": [
{
"id": "check1",
"status": "OK"
},
{
"id": "check2",
"status": "FAILED"
},
{
"id": "check3",
"status": "CONFIG_WARNING"
}
]
}'
How do I get the summary returned and the check that failed to return? I can get the failed check via something like:
echo "$json_string" | jq '.checks | .[] | select(.status != "OK")'
But I would really like to get the summary and the failed checks, or just the summary if there are no failures.
UPDATE The accepted answer pointed me in the right direction. Here is the command I ended up using to get the output formatted how I wanted it:
echo "$json_string" | jq '{"summary": .summary, "checks":[.checks[] | select(.status != "OK")]}'
This returns the summary and ids of the failed checks if there are any:
jq '.summary, (.checks[] | select(.status != "OK") | .id)' file.json