Home > Software engineering >  JQ get data from an array of array
JQ get data from an array of array

Time:10-08

How can I get a value from an array of arrays. This is JSON from which I have to get the data:

{
  "status": "success",
  "data": {
    "resultType": "matrix",
    "result": [
      {
        "metric": {
          "myMetric": "ABC"
        },
        "values": [
          [
            1633597734,
            "64.54166666666667"
          ],
          [
            1633598034,
            "65.51666666666667"  <-- wanted value
          ]
        ]
      }
    ]
  }
}

I have to get value from the last array in values. I tried the following:

jq .data.result.values input.json
jq .data.result.values[] input.json
jq .data.result.values[][] input.json

For every one the result is:

jq: error (at json:0): Cannot index array with string "values"

How can I get value from the last array in values?

CodePudding user response:

You can use a negative index to enumerate from the back of an array: to get the last element of the last array in the values of the last element of result:

.data.result[-1].values[-1][-1]
  • Related