Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move nested value in jq

Tags:

json

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..

like image 354
Enrichman Avatar asked Sep 18 '26 00:09

Enrichman


1 Answers

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 )
like image 186
peak Avatar answered Sep 20 '26 15:09

peak