JQ from_entries example

Build a JSON object from an array of {key, value} pairs with jq from_entries. Interactive example that rebuilds an object keyed by a chosen field.

The jq expression

.countries[] | [{key:.name, value: {id, population}}] | from_entries

from_entries is the inverse of to_entries: it takes an array of {key, value} objects and builds an object from them. It is the go-to way to re-key data by a field. Here we turn each country into a {key, value} pair using .name as the key and then assemble a single object keyed by country name.