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

The entire object gets duplicated in output when trying to parse array nested inside object using jq

$
0
0

I have the following json data:

{"Country": "India","States": [    {"Name": "TN","Cities": ["city1","city2"      ]    },    {"Name": "KL","Cities": ["city3","city4"      ]    }  ]}

jq expression:

{  country:.Country,states: .States | (    foreach .[] as $item (0;       {       name:$item.Name,       city:$item.Cities      }     )  )}

I get the following output:

{"country": "India","states": {"name": "TN","city": ["city1","city2"    ]  }}{"country": "India","states": {"name": "KL","city": ["city3","city4"    ]  }}

I don’t want the country to be repeated two times. The main requirement is to change the key names, like city instead of Cities


Viewing all articles
Browse latest Browse all 527

Trending Articles



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