Check whether any value is zero using jq and return 0 to the calling shell script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have this JSON file:




"key11":1010,
"key12":12120,
"key13":12103



How can I use jq to check whether any of the values corresponding to a key[0-9][0-9] are zero, making jq exit successfully if any is and not otherwise?



I have this script from a previous question



#!/bin/bash
json=$(cat <<EOF
"key11":12120,"key11_":13,"key11_yes":12107,
"key12":13492,"key12_no":9,"key12_yes":13483,
"key13":10200,"key13_no":9,"key13_yes":10191,
"key21":16756,"key21_no":30,"key21_yes":16726,
"key22":17057,"key22_no":34,"key22_yes":17023,
"key23":16798,"key23_no":25,"key23_yes":16773,
"key31":2733,"key31_yes":2733,
"key32":2561,"key32_yes":2561,
"key33":2570,"key33_yes":2570
EOF
)
json2=$(echo $json|jq 'with_entries(if (.key|test("key[0-9][0-9]$")) then ( key: .key, value: .value ) else empty end )')


The result is:



 "key11": 12120, "key12": 13492, "key13": 10200, "key21": 16756, "key22": 17057, "key23": 16798, "key31": 2733, "key32": 2561, "key33": 2570 


Now I want check the value attached to all keys in $json2, and then if the value of any of the entries is zero return 0.







share|improve this question

















  • 1




    What does "value keys are zero" mean?
    – Michael Homer
    May 19 at 6:46










  • You seem to have an existing script, could you please share it with us so that we can see what you're trying to do?
    – Kusalananda
    May 19 at 6:47










  • e.g : "key11":1010, "key12":12120, "key13":12103, "key14": 0 If the value of each of the keys is zero, script return 0 status.
    – pyramid13
    May 19 at 6:49










  • @pyramid13, elaborate your condition: it should return 0 if ANY of keys is 0?
    – RomanPerekhrest
    May 19 at 6:59










  • @Kusalananda, The script was shared :)
    – pyramid13
    May 19 at 7:01














up vote
0
down vote

favorite












I have this JSON file:




"key11":1010,
"key12":12120,
"key13":12103



How can I use jq to check whether any of the values corresponding to a key[0-9][0-9] are zero, making jq exit successfully if any is and not otherwise?



I have this script from a previous question



#!/bin/bash
json=$(cat <<EOF
"key11":12120,"key11_":13,"key11_yes":12107,
"key12":13492,"key12_no":9,"key12_yes":13483,
"key13":10200,"key13_no":9,"key13_yes":10191,
"key21":16756,"key21_no":30,"key21_yes":16726,
"key22":17057,"key22_no":34,"key22_yes":17023,
"key23":16798,"key23_no":25,"key23_yes":16773,
"key31":2733,"key31_yes":2733,
"key32":2561,"key32_yes":2561,
"key33":2570,"key33_yes":2570
EOF
)
json2=$(echo $json|jq 'with_entries(if (.key|test("key[0-9][0-9]$")) then ( key: .key, value: .value ) else empty end )')


The result is:



 "key11": 12120, "key12": 13492, "key13": 10200, "key21": 16756, "key22": 17057, "key23": 16798, "key31": 2733, "key32": 2561, "key33": 2570 


Now I want check the value attached to all keys in $json2, and then if the value of any of the entries is zero return 0.







share|improve this question

















  • 1




    What does "value keys are zero" mean?
    – Michael Homer
    May 19 at 6:46










  • You seem to have an existing script, could you please share it with us so that we can see what you're trying to do?
    – Kusalananda
    May 19 at 6:47










  • e.g : "key11":1010, "key12":12120, "key13":12103, "key14": 0 If the value of each of the keys is zero, script return 0 status.
    – pyramid13
    May 19 at 6:49










  • @pyramid13, elaborate your condition: it should return 0 if ANY of keys is 0?
    – RomanPerekhrest
    May 19 at 6:59










  • @Kusalananda, The script was shared :)
    – pyramid13
    May 19 at 7:01












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have this JSON file:




"key11":1010,
"key12":12120,
"key13":12103



How can I use jq to check whether any of the values corresponding to a key[0-9][0-9] are zero, making jq exit successfully if any is and not otherwise?



I have this script from a previous question



#!/bin/bash
json=$(cat <<EOF
"key11":12120,"key11_":13,"key11_yes":12107,
"key12":13492,"key12_no":9,"key12_yes":13483,
"key13":10200,"key13_no":9,"key13_yes":10191,
"key21":16756,"key21_no":30,"key21_yes":16726,
"key22":17057,"key22_no":34,"key22_yes":17023,
"key23":16798,"key23_no":25,"key23_yes":16773,
"key31":2733,"key31_yes":2733,
"key32":2561,"key32_yes":2561,
"key33":2570,"key33_yes":2570
EOF
)
json2=$(echo $json|jq 'with_entries(if (.key|test("key[0-9][0-9]$")) then ( key: .key, value: .value ) else empty end )')


The result is:



 "key11": 12120, "key12": 13492, "key13": 10200, "key21": 16756, "key22": 17057, "key23": 16798, "key31": 2733, "key32": 2561, "key33": 2570 


Now I want check the value attached to all keys in $json2, and then if the value of any of the entries is zero return 0.







share|improve this question













I have this JSON file:




"key11":1010,
"key12":12120,
"key13":12103



How can I use jq to check whether any of the values corresponding to a key[0-9][0-9] are zero, making jq exit successfully if any is and not otherwise?



I have this script from a previous question



#!/bin/bash
json=$(cat <<EOF
"key11":12120,"key11_":13,"key11_yes":12107,
"key12":13492,"key12_no":9,"key12_yes":13483,
"key13":10200,"key13_no":9,"key13_yes":10191,
"key21":16756,"key21_no":30,"key21_yes":16726,
"key22":17057,"key22_no":34,"key22_yes":17023,
"key23":16798,"key23_no":25,"key23_yes":16773,
"key31":2733,"key31_yes":2733,
"key32":2561,"key32_yes":2561,
"key33":2570,"key33_yes":2570
EOF
)
json2=$(echo $json|jq 'with_entries(if (.key|test("key[0-9][0-9]$")) then ( key: .key, value: .value ) else empty end )')


The result is:



 "key11": 12120, "key12": 13492, "key13": 10200, "key21": 16756, "key22": 17057, "key23": 16798, "key31": 2733, "key32": 2561, "key33": 2570 


Now I want check the value attached to all keys in $json2, and then if the value of any of the entries is zero return 0.









share|improve this question












share|improve this question




share|improve this question








edited May 19 at 8:03
























asked May 19 at 6:44









pyramid13

779




779







  • 1




    What does "value keys are zero" mean?
    – Michael Homer
    May 19 at 6:46










  • You seem to have an existing script, could you please share it with us so that we can see what you're trying to do?
    – Kusalananda
    May 19 at 6:47










  • e.g : "key11":1010, "key12":12120, "key13":12103, "key14": 0 If the value of each of the keys is zero, script return 0 status.
    – pyramid13
    May 19 at 6:49










  • @pyramid13, elaborate your condition: it should return 0 if ANY of keys is 0?
    – RomanPerekhrest
    May 19 at 6:59










  • @Kusalananda, The script was shared :)
    – pyramid13
    May 19 at 7:01












  • 1




    What does "value keys are zero" mean?
    – Michael Homer
    May 19 at 6:46










  • You seem to have an existing script, could you please share it with us so that we can see what you're trying to do?
    – Kusalananda
    May 19 at 6:47










  • e.g : "key11":1010, "key12":12120, "key13":12103, "key14": 0 If the value of each of the keys is zero, script return 0 status.
    – pyramid13
    May 19 at 6:49










  • @pyramid13, elaborate your condition: it should return 0 if ANY of keys is 0?
    – RomanPerekhrest
    May 19 at 6:59










  • @Kusalananda, The script was shared :)
    – pyramid13
    May 19 at 7:01







1




1




What does "value keys are zero" mean?
– Michael Homer
May 19 at 6:46




What does "value keys are zero" mean?
– Michael Homer
May 19 at 6:46












You seem to have an existing script, could you please share it with us so that we can see what you're trying to do?
– Kusalananda
May 19 at 6:47




You seem to have an existing script, could you please share it with us so that we can see what you're trying to do?
– Kusalananda
May 19 at 6:47












e.g : "key11":1010, "key12":12120, "key13":12103, "key14": 0 If the value of each of the keys is zero, script return 0 status.
– pyramid13
May 19 at 6:49




e.g : "key11":1010, "key12":12120, "key13":12103, "key14": 0 If the value of each of the keys is zero, script return 0 status.
– pyramid13
May 19 at 6:49












@pyramid13, elaborate your condition: it should return 0 if ANY of keys is 0?
– RomanPerekhrest
May 19 at 6:59




@pyramid13, elaborate your condition: it should return 0 if ANY of keys is 0?
– RomanPerekhrest
May 19 at 6:59












@Kusalananda, The script was shared :)
– pyramid13
May 19 at 7:01




@Kusalananda, The script was shared :)
– pyramid13
May 19 at 7:01










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










any is a built-in filter that returns true if any item is true, and false otherwise. --exit-status/-e causes jq to exit 0 iff the last output is neither false nor null.



If you've already winnowed the document down to just the keys you care about like in your script, you can just do this:



previous_command | jq -e 'any( . ; . == 0 )'


. pulls all the values out, and any will output true only if it finds a zero in them. Alternatively, [.|.==0]|any or map(.==0) | any; pick whichever one you find clearest.




You can also do the whole thing in one go, saving a wasted jq process:



jq -e '[to_entries | . | select(.key | test("key[0-9][0-9]$")) | .value == 0 ] | any' data.json


This selects all the matching keys and checks whether each value is 0, then pipes the collection of results through any, thus exiting 0 if there were any zeros. If you're sure there are no NaNs you could also multiply them all together with reduce.






share|improve this answer























  • If we want to look at the exact format of a keys, what should we do? i use json2=$(echo $json|jq -e '[to_entries | . | (.key | test("key[0-9][0-9]$"))] | any ') but return true even if we have only key.
    – pyramid13
    May 19 at 8:55











  • @pyramid13, important to quote the variable: echo "$json" not echo $json. To illustrate, set json="foo * bar" and try the 2 echo commands.
    – glenn jackman
    May 19 at 12:56











  • @Michael Homer, please look my qustion
    – pyramid13
    May 21 at 7:14










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444755%2fcheck-whether-any-value-is-zero-using-jq-and-return-0-to-the-calling-shell-scrip%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










any is a built-in filter that returns true if any item is true, and false otherwise. --exit-status/-e causes jq to exit 0 iff the last output is neither false nor null.



If you've already winnowed the document down to just the keys you care about like in your script, you can just do this:



previous_command | jq -e 'any( . ; . == 0 )'


. pulls all the values out, and any will output true only if it finds a zero in them. Alternatively, [.|.==0]|any or map(.==0) | any; pick whichever one you find clearest.




You can also do the whole thing in one go, saving a wasted jq process:



jq -e '[to_entries | . | select(.key | test("key[0-9][0-9]$")) | .value == 0 ] | any' data.json


This selects all the matching keys and checks whether each value is 0, then pipes the collection of results through any, thus exiting 0 if there were any zeros. If you're sure there are no NaNs you could also multiply them all together with reduce.






share|improve this answer























  • If we want to look at the exact format of a keys, what should we do? i use json2=$(echo $json|jq -e '[to_entries | . | (.key | test("key[0-9][0-9]$"))] | any ') but return true even if we have only key.
    – pyramid13
    May 19 at 8:55











  • @pyramid13, important to quote the variable: echo "$json" not echo $json. To illustrate, set json="foo * bar" and try the 2 echo commands.
    – glenn jackman
    May 19 at 12:56











  • @Michael Homer, please look my qustion
    – pyramid13
    May 21 at 7:14














up vote
3
down vote



accepted










any is a built-in filter that returns true if any item is true, and false otherwise. --exit-status/-e causes jq to exit 0 iff the last output is neither false nor null.



If you've already winnowed the document down to just the keys you care about like in your script, you can just do this:



previous_command | jq -e 'any( . ; . == 0 )'


. pulls all the values out, and any will output true only if it finds a zero in them. Alternatively, [.|.==0]|any or map(.==0) | any; pick whichever one you find clearest.




You can also do the whole thing in one go, saving a wasted jq process:



jq -e '[to_entries | . | select(.key | test("key[0-9][0-9]$")) | .value == 0 ] | any' data.json


This selects all the matching keys and checks whether each value is 0, then pipes the collection of results through any, thus exiting 0 if there were any zeros. If you're sure there are no NaNs you could also multiply them all together with reduce.






share|improve this answer























  • If we want to look at the exact format of a keys, what should we do? i use json2=$(echo $json|jq -e '[to_entries | . | (.key | test("key[0-9][0-9]$"))] | any ') but return true even if we have only key.
    – pyramid13
    May 19 at 8:55











  • @pyramid13, important to quote the variable: echo "$json" not echo $json. To illustrate, set json="foo * bar" and try the 2 echo commands.
    – glenn jackman
    May 19 at 12:56











  • @Michael Homer, please look my qustion
    – pyramid13
    May 21 at 7:14












up vote
3
down vote



accepted







up vote
3
down vote



accepted






any is a built-in filter that returns true if any item is true, and false otherwise. --exit-status/-e causes jq to exit 0 iff the last output is neither false nor null.



If you've already winnowed the document down to just the keys you care about like in your script, you can just do this:



previous_command | jq -e 'any( . ; . == 0 )'


. pulls all the values out, and any will output true only if it finds a zero in them. Alternatively, [.|.==0]|any or map(.==0) | any; pick whichever one you find clearest.




You can also do the whole thing in one go, saving a wasted jq process:



jq -e '[to_entries | . | select(.key | test("key[0-9][0-9]$")) | .value == 0 ] | any' data.json


This selects all the matching keys and checks whether each value is 0, then pipes the collection of results through any, thus exiting 0 if there were any zeros. If you're sure there are no NaNs you could also multiply them all together with reduce.






share|improve this answer















any is a built-in filter that returns true if any item is true, and false otherwise. --exit-status/-e causes jq to exit 0 iff the last output is neither false nor null.



If you've already winnowed the document down to just the keys you care about like in your script, you can just do this:



previous_command | jq -e 'any( . ; . == 0 )'


. pulls all the values out, and any will output true only if it finds a zero in them. Alternatively, [.|.==0]|any or map(.==0) | any; pick whichever one you find clearest.




You can also do the whole thing in one go, saving a wasted jq process:



jq -e '[to_entries | . | select(.key | test("key[0-9][0-9]$")) | .value == 0 ] | any' data.json


This selects all the matching keys and checks whether each value is 0, then pipes the collection of results through any, thus exiting 0 if there were any zeros. If you're sure there are no NaNs you could also multiply them all together with reduce.







share|improve this answer















share|improve this answer



share|improve this answer








edited May 19 at 7:35


























answered May 19 at 7:03









Michael Homer

42.3k6108148




42.3k6108148











  • If we want to look at the exact format of a keys, what should we do? i use json2=$(echo $json|jq -e '[to_entries | . | (.key | test("key[0-9][0-9]$"))] | any ') but return true even if we have only key.
    – pyramid13
    May 19 at 8:55











  • @pyramid13, important to quote the variable: echo "$json" not echo $json. To illustrate, set json="foo * bar" and try the 2 echo commands.
    – glenn jackman
    May 19 at 12:56











  • @Michael Homer, please look my qustion
    – pyramid13
    May 21 at 7:14
















  • If we want to look at the exact format of a keys, what should we do? i use json2=$(echo $json|jq -e '[to_entries | . | (.key | test("key[0-9][0-9]$"))] | any ') but return true even if we have only key.
    – pyramid13
    May 19 at 8:55











  • @pyramid13, important to quote the variable: echo "$json" not echo $json. To illustrate, set json="foo * bar" and try the 2 echo commands.
    – glenn jackman
    May 19 at 12:56











  • @Michael Homer, please look my qustion
    – pyramid13
    May 21 at 7:14















If we want to look at the exact format of a keys, what should we do? i use json2=$(echo $json|jq -e '[to_entries | . | (.key | test("key[0-9][0-9]$"))] | any ') but return true even if we have only key.
– pyramid13
May 19 at 8:55





If we want to look at the exact format of a keys, what should we do? i use json2=$(echo $json|jq -e '[to_entries | . | (.key | test("key[0-9][0-9]$"))] | any ') but return true even if we have only key.
– pyramid13
May 19 at 8:55













@pyramid13, important to quote the variable: echo "$json" not echo $json. To illustrate, set json="foo * bar" and try the 2 echo commands.
– glenn jackman
May 19 at 12:56





@pyramid13, important to quote the variable: echo "$json" not echo $json. To illustrate, set json="foo * bar" and try the 2 echo commands.
– glenn jackman
May 19 at 12:56













@Michael Homer, please look my qustion
– pyramid13
May 21 at 7:14




@Michael Homer, please look my qustion
– pyramid13
May 21 at 7:14












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444755%2fcheck-whether-any-value-is-zero-using-jq-and-return-0-to-the-calling-shell-scrip%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)