Dreen
Dreen

Reputation: 7236

Select nth element from multidimensional JSON array with jq

How can I use jq to transform this array of arrays:

[
  [
    "sequence",
    "int"
  ],
  [
    "time",
    "string"
  ],
  ...
]

Into an array that contains the first (0) element from every subarray? Meaning to produce output like this:

[
    "sequence",
    "time",
    ...
]

I was thinking to use reduce xx as $item (...) but I didnt manage to come up with anything useful.

Upvotes: 9

Views: 5466

Answers (3)

jq170727
jq170727

Reputation: 14715

Here is a solution using reduce

reduce .[] as $k ( null; . + [$k[0]] )

Upvotes: 0

rdm
rdm

Reputation: 688

Another option would be jq '[.[][0]]'

this gives the same result as using map(.[0])

Upvotes: 1

Psylone
Psylone

Reputation: 2808

You can use map filter this way:

jq 'map(.[0])'

Upvotes: 5

Related Questions