Given a json file in the format as :
[
{
name : "A",
value : "1"
},
{
name : "B",
value : "5"
},
{
name : "E",
value : "8"
}
]
How would I convert it to something like this using jq:
{
"A" : {
name : "A",
value : "1"
},
"B" : {
name : "B",
value : "5"
},
"E" : {
name : "E",
value : "8"
}
}
jq '{(.[].name) : "the name"}' 'myfile.json'
gets me an object with [].name keys but how do I assign the object to it?
map( { (.name|tostring): . } ) | add
(The tostring
is for safety/robustness.)
If your jq has INDEX/1
(introduced after the release of version 1.5), you can simply write:
INDEX(.name)
Just build up a new object going through the items in the array. Add the items to the object with the name
as the key.
reduce .[] as $i ({}; .[$i.name] = $i)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With