Home > Software design >  jq: mapping all values of an object
jq: mapping all values of an object

Time:01-07

Say I have an object like so:

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

I want to use jq to convert this to:

{
  "key1": {
    "innerkey": "value1"
  },
  "key2": {
    "innerkey": "value2"
  },
  "key3": {
    "innerkey": "value3"
  }
}

i.e. I want to apply a mapping to every value in the object, that converts $value to {"innerkey": $value}. How can I achieve this with jq?

CodePudding user response:

It's literally called map_values. Use it like this

map_values({innerkey:.})

Demo

CodePudding user response:

You could also use the fact that iterating over an object iterates its values. So you could update those values on the object.

.[] |= {innerkey:.}

jqplay

  •  Tags:  
  • Related