Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jq use any() / all() to check if certain 'key' exists and returns a bool (true/false) as one final result

I want to use jq to check if a ‘keyword’ exists as a json ‘key’ in all paths(any level), here is what I came up with:

jq -c 'paths | select(.[-1]) as $p| index("headline") //empty' news.json

The output is in an array format:

6
7
9

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to map the output array to jq function any() and get an ‘overall’ result as ‘true'(if key exists) or ‘false’ if no such key exists
How can I get that done with jq ?

>Solution :

Provide any with an iterator and a condition

jq 'any(paths; .[-1] == "headline")'

You can also provide the -e (or --exit-status) option to have an exit status of 0 if the result was true, and 1 if the result was false, which can immediately be used for further processing in the shell.

jq -e 'any(paths; .[-1] == "headline")'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading