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

Json Object Parsing using shell script

$
0
0

I am trying to parse below json object using shell script:

country.json

 {"countries": [            {"country":"India","city":["India1","India2","India3"]},            {"country":"USA","city":["USA1","USA2","USA3"]}           ]    }

and my desired output should be like:

country:India, city:India1country:India, city:India2country:India, city:India3country:USA, city:USA1country:USA, city:USA2country:USA, city:USA3 

I am using jq in shell script to iterate the above json like this:

for k in $(jq '.countries | keys | .[]' country.json); do    countryObj=$(jq -r ".countries[$k]" country.json);    countryValue=$(jq -r '.country'<<< "$countryObj");    city_array=$(jq -r '.city'<<< "$countryObj");    echo $city_arraydone 

from this I am able to get city_array i.e. ["India1","India2","India3"] and ["USA1","USA2","USA3"] but I am not able to get desired output mentioned above


Viewing all articles
Browse latest Browse all 657

Trending Articles



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