Johnny Metz
Johnny Metz

Reputation: 5965

jq grab keys from array of objects

Let's say I have the following array:

[
  {
    "a": 1,
    "b": 2,
    "c": 3,
    "d": 4
  },
  {
    "a": 10,
    "b": 20,
    "c": 30,
    "d": 40
  },
  {
    "a": 100,
    "b": 200,
    "c": 300,
    "d": 400
  }
]

I want to generate a new array with just the a and b keys. I know I can do it like this:

ARRAY | jq '. | map({ a: .a, b: .b })'

I'm not renaming any of the keys so having to type a and b seems a bit verbose. Is there a cleaner way to do this?

Upvotes: 1

Views: 52

Answers (1)

pmf
pmf

Reputation: 36151

You will have to type a and b once in order to specify which ones to keep:

jq 'map({a,b})'

Alternatively, specify what to delete, here .c and .d:

jq 'map(del(.c,.d))'

In any case, .| is also superfluous.

Upvotes: 2

Related Questions