Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 657

JQ and Multiple Fields with a SELECT

$
0
0

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.

UPDATEThe 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")]}'

Viewing all articles
Browse latest Browse all 657

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>