Daniel Burke
Daniel Burke

Reputation: 13

JQ: Print Key and Attribute Value

I've been trying to print a key and attribute value so given this JSON

[
  {
    "key": "foo",
    "value": { "sub" : false}
  },
  {
    "key": "bar",
    "value": { "sub" : true}
  }
]

I want to return something like this:

[
  {
    "foo": "false"
  },
  {
    "bar": "true"
  }
]

I'm using to_entries to do this but when I feed the JSON into JQ though I can't seem to access it correctly? I get an error:

jq: error (at :9): Cannot index array with string "key"

This test case simplifies the behaviour

Upvotes: 1

Views: 1431

Answers (1)

Jeff Mercado
Jeff Mercado

Reputation: 134841

You can't really use to_entries here, that creates an object out of an array of key/value objects. You're trying to map a key/value object to another object. Just map it directly.

map({ (.key): .value.sub })

Upvotes: 2

Related Questions