Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving jq results as one list object, vs a stream of members

Hi i am trying in shell script to get a list of consul service tags for myApp passing nodes, using jq.

when i have something like this..

curl 'http://127.0.0.1:8500/v1/health/service/myApp?dc=a&passing=true' \
  | jq '.[].Checks[1].ServiceTags[]' 

output is..

"a"
"b"
"c"
"d"

I want to know if i could get it as a list. I want the output in this way..

 ["a","b","c","d"]

can you please suggest options..

like image 861
yregex2017 Avatar asked Oct 28 '25 13:10

yregex2017


1 Answers

Instead of

jq '.[].Checks[1].ServiceTags[]' 

Try

jq '[.[].Checks[1].ServiceTags[]]' 

The extra [] instruct jq to collect the values into an array.
See Array construction: [] in the jq Manual.

like image 190
jq170727 Avatar answered Oct 31 '25 05:10

jq170727



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!