1

I have a following json

    {
    "status": "success",
    "data": {
            "resultType": "matrix",
            "result": [{
                    "metric": {},
                    "values": [[1573452693.024, "36380.58030773418"], [1573452707.024, "51397.82785694454"], [1573452721.024, "38711.55804872829"], [1573452735.024, "47801.74418514242"], [1573452749.024, "42140.81258908656"]]
                    }]
            }
    }

Using jq I extract values in with following way:

    curl "LINK" | .\\jq.exe -c '.data.result[].values'

But that returns string and I need to iterate through received array. What I need to do with these values: 1. Get pairs; 2. Change Unix TimeStamp to readable one; 3. Save in CSV. How can I extract get the array as the output?

Edit for comment in Thor answer with expanded json:

{
    "status": "success",
    "data": {
        "resultType": "matrix",
        "result": [{
            "metric": {"container_name":"name1"},
            "values": [[1573452693.024, "36380.58030773418"], [1573452707.024, "51397.82785694454"], [1573452721.024, "38711.55804872829"], [1573452735.024, "47801.74418514242"], [15734 52749.024, "42140.81258908656"]]
        },{
            "metric": {"container_name":"name2"},
            "values": [[1573452693.024, "36380.58030773418"], [1573452707.024, "51397.82785694454"], [1573452721.024, "38711.55804872829"], [1573452735.024, "47801.74418514242"], [15734 52749.024, "42140.81258908656"]]
        },{
            "metric": {"container_name":"name3"},
            "values": [[1573452693.024, "36380.58030773418"], [1573452707.024, "51397.82785694454"], [1573452721.024, "38711.55804872829"], [1573452735.024, "47801.74418514242"], [15734 52749.024, "42140.81258908656"]]
        }
        ]
    }
}
5
  • 1
    Any command would always give you a string as output. You cannot get a bash array from it. You can split the string to an array. OR you can get a count and loop to get n-th element of the result in each iteration. I am not sure I understood your requirement correctly. Please elaborate what you want to do with the individual elements. Commented Nov 11, 2019 at 10:36
  • I'll add it to my question: I want to get values, iterate through pairs, "translate" 1st (left) element from Unix timespamp to readable one and save it into CSV file Commented Nov 11, 2019 at 10:42
  • 1
    might be easier to write a python script do achieve this, rather that doing all in bash. Commented Nov 11, 2019 at 11:13
  • I see that result is an array. Will it have more than 1 elements in any case, or will it always have only 1 entry as your example has? Commented Nov 12, 2019 at 4:43
  • 1
    @DarrenSmith, I have the same opinion. Especially given that the data can be directly assigned to python as dictionary. Commented Nov 12, 2019 at 4:47

1 Answer 1

4

You could do all of it in jq, e.g.:

jq -r '.data.result[].values | .[] | .[0] |= strftime("%Y-%m-%d %H:%M") | @csv'

Output:

"2019-11-11 06:11","36380.58030773418"
"2019-11-11 06:11","51397.82785694454"
"2019-11-11 06:12","38711.55804872829"
"2019-11-11 06:12","47801.74418514242"
"2019-11-11 06:12","42140.81258908656"
Sign up to request clarification or add additional context in comments.

3 Comments

What about if I want to iterate through results? Metric contains "container_name":"name", values contain array of values for container. I'd like to put it in CSV ordered by container_name & time in array. Example (for readability I've put into question
To further clarify. I make a curl call to get specific values. Devs now want me to make more generic call that returns multiple metrics with values. It is all from the same graph so every array of values has the same number of values with the same time. That's why I intend to put them within the same CSV file ordered by time & metrics (here container_name)
@Uliysess: This is a different issue. I suggest you post a new question

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.