9

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.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
Dreen
  • 6,976
  • 11
  • 47
  • 69
  • 1
    jq is a C program. Why tag as jquery? – Lee Meador Sep 16 '13 at 21:59
  • Did you mean jQuery by jq? – MahanGM Sep 16 '13 at 22:07
  • I tried to tag as jq, but now i see that if you do that, SO changes the tag to jquery... you cant actually tag something jq – Dreen Sep 17 '13 at 12:45
  • @Dreen - For future reference, since [`jq` is defined as a synonym for `jquery`](http://stackoverflow.com/tags/jquery/synonyms) in Stack Overflow's tag system, it gets changed when you submit the question. I don't have the rights to undo this (@SomeoneReadingThisWhoDoesHaveThisPrivilege - I think it could be done, as the `jq` synonym has never been used) I have fixed you up with a brand new `jq-json-processor` tag instead. – Mathijs Flietstra Sep 19 '13 at 14:45

3 Answers3

5

You can use map filter this way:

jq 'map(.[0])'
Psylone
  • 2,788
  • 1
  • 18
  • 15
1

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

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

Buddy
  • 10,874
  • 5
  • 41
  • 58
rdm
  • 658
  • 5
  • 16
0

Here is a solution using reduce

reduce .[] as $k ( null; . + [$k[0]] )
jq170727
  • 13,159
  • 3
  • 46
  • 56