Home > Software design >  jq map first level key to a nested value
jq map first level key to a nested value

Time:12-11

Using jq, how can I transform an input to a JSON object which maps the first level keys to the value of the latest nested value? For example, given the input

{
  "foo": {
    "current": "0.15.14",
    "wanted": "0.15.14",
    "latest": "0.16.4",
  },
  "bar": {
    "current": "8.27.0",
    "wanted": "8.27.0",
    "latest": "8.29.0",
  },
  "baz": {
    "current": "27.1.5",
    "wanted": "27.1.5",
    "latest": "27.1.6",
  }
}

How can it be transformed to

{
  "foo": "0.16.4",
  "bar": "8.29.0",
  "baz": "27.1.6"
}

CodePudding user response:

Map all field values to their .latest field, either by using map_values

map_values(.latest)

Demo

Or by updating |= each field .[] with that filter

.[] |= .latest

Demo

Actually, map_values(f) is just a defined shortcut for .[] |= f.

  • Related