Enrichman
Enrichman

Reputation: 11337

Move nested value in jq

I have a json like this:

{
  "key_1": {
    "type": "string",
    "value": "foo"
  },
  "key_2": {
    "type": "string",
    "value": "bar"
  }
}

that I would like to output like this

{
  "key_1": "foo",
  "key_2": "bar"
}

jq '.[].value' will give me the values:

"foo"
"bar"

while this jq '(.[] = .[].value)' will give me

{
  "key_1": "foo",
  "key_2": "foo"
}
{
  "key_1": "bar",
  "key_2": "bar"
}

So I'm not sure..

Upvotes: 3

Views: 694

Answers (1)

peak
peak

Reputation: 116919

Since the task entails mapping the values of the top-level keys, map_values should come to mind:

map_values(.value)

You could also use with_entries, which might make sense if you wanted to manipulate the top-level keys as well:

with_entries( .value |= .value )

Upvotes: 5

Related Questions