From ES2015 with computed properties and Array.reduce/Array.map/Object.assign you can do:
[{name: 'foo', age: 43}, {name: 'bar', age: 55}].map(
o => ({[o.name]: o.age})).reduce((a, b) => Object.assign(a,b), {})
…and get:
{ foo: 43, bar: 55 }
How do I get this from JMESPath?
Attempt:
$echo '[{"name": "foo", "age": 43}, {"name": "bar", "age": 55}]' | jp [].{name:age}
[
{
"name": 43
},
{
"name": 55
}
]
[*].[@.name,@.age]
Returns
[['foo', 43], ['bar', 55]]
Which can then be post-processed outside of Jmespath, if that is an option for you.
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