Jordan
Jordan

Reputation: 4472

jq: mapping all values of an object

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?

Upvotes: 1

Views: 468

Answers (2)

Jeff Mercado
Jeff Mercado

Reputation: 134811

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

Upvotes: 0

pmf
pmf

Reputation: 36033

It's literally called map_values. Use it like this

map_values({innerkey:.})

Demo

Upvotes: 5

Related Questions