简体   繁体   中英

Getting Last Available element of a Particular Column in JQ

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

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)

Here's a different solution:

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

probably less efficient, but quite readable.

The difference to the other answer is that select(.) will filter any falsy value, not only non-existent values. So if your second columns were to contain false or null , these wouldn't show up in your result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM