Using JQ how do I convert array of objects to key-value pair object?
Given the JSON:
[
{
"name": "foo",
"id": "123"
},
{
"name": "bar",
"id": "456"
}
]
Desired output:
{
"foo": "123",
"bar": "456"
}
You can convert to an array of objects with key
and value
keys, then use from_entries
:
map({key: .name, value: .id}) | from_entries
--compact-output
(or -c
), but it'll remove all whitespace.
Commented
Jul 3 at 17:53
{ "foo": "123", "bar": "456" }
Commented
Jul 4 at 0:30