I've below JSON, I want to parse all these key names product, inventory, rating, review
dynamically and put it in the list and iterate the key name one by one.
{
"product": [
{
"productID": "123",
"productName": "test1"
}
],
"inventory": [
{
"id": "12093",
"name": "adie"
}
],
"rating": [
{
"value": "4",
"status": "done"
}
],
"review": [
{
"desc": "good",
"comments": "test"
}
]
}
After parsing the key name dynamically, I want to form all the key name in a comma separated and assigned to key_list
variable like below:
key_list="product,inventory,rating,review"
After that I want to iterate key_list
in a loop and print them or do something with that value. The desired output would be print the key name one by one in a loop:
product
inventory
rating
review
How can we achieve this using shell script jq?
CodePudding user response:
To produce a listing of the key names:
jq -r 'keys_unsorted[]'
To produce a comma-separated string with the key names, you could start with:
jq 'keys_unsorted|join(",")'