0

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?

4
  • Can you try with jq .data.result[0].values[1][1] input.json or if you want all the quoted floating point numbers jq .data.result[0].values[][1] input.json? Commented Oct 7, 2021 at 12:10
  • 2
    If you want the last entry in the last array in values of the last entry in result, this would work: .data.result[-1].values[-1][-1]; did you want just that, or for all values? Commented Oct 7, 2021 at 12:17
  • @MarkoE the first one is what I need and works fine, tnx Commented Oct 7, 2021 at 14:13
  • @BenjaminW. exactly what I need Commented Oct 7, 2021 at 14:13

2 Answers 2

2

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]
Sign up to request clarification or add additional context in comments.

Comments

0

demo: https://jqplay.org/s/YMpB8-A4-Q

filter: 
.data.result[].values[1][1]?

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

output:
"65.51666666666667"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.