Reputation: 309
I have a JSON file like this:
{
"images1" :
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "[email protected]",
"scale" : "2x"
},
"images2" :
{
"size" : "29x30",
"idiom" : "iphone2",
"filename" : "[email protected]",
"scale" : "22x"
}
}
I will pass JSON object name as an input. So if I know "images1" is the object, then I need all the keys and values of that object to be stored in two separate arrays, so that I can make use of them in further processing.
Any help is greatly appreciated. Thanks
Upvotes: 0
Views: 210
Reputation: 24812
You can use the following :
jq ".$1 | { keys: keys_unsorted, values: [.[]] }"
where $1
should provide the name of the item you want to address (note that this assume you're using this in a script. You will probably want to use fedorqui's alternative instead).
It will produce an object whose keys
property will be an array of the keys of $1
and values
an array of the associated values :
$ echo '{
"images1" :
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "[email protected]",
"scale" : "2x"
},
"images2" :
{
"size" : "29x30",
"idiom" : "iphone2",
"filename" : "[email protected]",
"scale" : "22x"
}
}' | jq ".images1 | { keys: keys_unsorted, values: [.[]] }"
{
"keys": [
"size",
"idiom",
"filename",
"scale"
],
"values": [
"29x29",
"iphone",
"[email protected]",
"2x"
]
}
Upvotes: 1