Home > OS >  Getting Last Available element of a Particular Column in JQ
Getting Last Available element of a Particular Column in JQ

Time:09-06

I have been working on a Bash script. I am having a curl response as follows.

{
  "range": "'PR-DETAILS'!A1:Z1000",
  "majorDimension": "ROWS",
  "values": [
    [
      "PR ID",
      "PR Owner"
    ],
    [
      "1929",
      "Angel"
    ],
    [
      "73",    
      "Martin"
    ],
    [
      "142"
    ]
  ]
}

Here I just want to get the last available element for the second column.

Expected Answer:- Martin

CodePudding user response:

Here's one way:

last(.values[] | select(has(1))) [1]

Online demo

If the second column does not contain false values (null, false) this will also work:

last(.values[][1] // empty)

CodePudding user response:

Here's a different solution:

.values | map(.[1] | select(.)) | last

probably less efficient, but quite readable

  • Related