Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq map values from different arrays

Tags:

json

jq

I want to parse the following json array that comes out of AWS GetMetricData API. It contains the timestamps and values in different arrays:

{
    "Messages": [],
    "MetricDataResults": [
        {
            "Timestamps": [
                "2021-12-28T02:11:00Z",
                "2021-12-28T02:10:00Z",
                "2021-12-28T02:09:00Z",
                "2021-12-28T02:08:00Z",
                "2021-12-28T02:07:00Z",
                "2021-12-28T02:06:00Z",
                "2021-12-28T02:05:00Z",
                "2021-12-28T02:04:00Z",
                "2021-12-28T02:03:00Z",
                "2021-12-28T02:02:00Z",
                "2021-12-28T02:01:00Z",
                "2021-12-28T02:00:00Z",
                "2021-12-28T01:59:00Z",
                "2021-12-28T01:58:00Z",
                "2021-12-28T01:57:00Z",
                "2021-12-28T01:56:00Z",
                "2021-12-28T01:55:00Z",
                "2021-12-28T01:54:00Z",
                "2021-12-28T01:53:00Z",
                "2021-12-28T01:52:00Z",
                "2021-12-28T01:51:00Z"
            ],
            "StatusCode": "Complete",
            "Values": [
                5.0,
                2.0,
                2.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                0.0
            ],
            "Id": "groupDesiredCapacity",
            "Label": "myRequestLabel"
        },
        {
            "Timestamps": [
                "2021-12-28T02:11:00Z",
                "2021-12-28T02:10:00Z",
                "2021-12-28T02:09:00Z",
                "2021-12-28T02:08:00Z",
                "2021-12-28T02:07:00Z",
                "2021-12-28T02:06:00Z",
                "2021-12-28T02:05:00Z",
                "2021-12-28T02:04:00Z",
                "2021-12-28T02:03:00Z",
                "2021-12-28T02:02:00Z",
                "2021-12-28T02:01:00Z",
                "2021-12-28T02:00:00Z",
                "2021-12-28T01:59:00Z",
                "2021-12-28T01:58:00Z",
                "2021-12-28T01:57:00Z",
                "2021-12-28T01:56:00Z",
                "2021-12-28T01:55:00Z",
                "2021-12-28T01:54:00Z",
                "2021-12-28T01:53:00Z",
                "2021-12-28T01:52:00Z",
                "2021-12-28T01:51:00Z"
            ],
            "StatusCode": "Complete",
            "Values": [
                5.0,
                2.0,
                2.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                0.0
            ],
            "Id": "groupInServiceCapacity",
            "Label": "myRequestLabel"
        },
        {
            "Timestamps": [
                "2021-12-28T02:11:00Z",
                "2021-12-28T02:10:00Z",
                "2021-12-28T02:09:00Z",
                "2021-12-28T02:08:00Z",
                "2021-12-28T02:07:00Z",
                "2021-12-28T02:06:00Z",
                "2021-12-28T02:05:00Z",
                "2021-12-28T02:04:00Z",
                "2021-12-28T02:03:00Z",
                "2021-12-28T02:02:00Z",
                "2021-12-28T02:01:00Z",
                "2021-12-28T02:00:00Z",
                "2021-12-28T01:59:00Z",
                "2021-12-28T01:58:00Z",
                "2021-12-28T01:57:00Z",
                "2021-12-28T01:56:00Z",
                "2021-12-28T01:55:00Z",
                "2021-12-28T01:54:00Z",
                "2021-12-28T01:53:00Z",
                "2021-12-28T01:52:00Z"
            ],
            "StatusCode": "Complete",
            "Values": [
                94.20519316022799,
                97.325,
                99.95833333333333,
                99.94166666666666,
                97.35833333333333,
                62.375,
                96.61666666666666,
                0.3916666666666666,
                0.3833525009583812,
                0.391647084312451,
                24.05873431223854,
                60.84898585023583,
                64.54059099015015,
                25.07541792363206,
                0.25,
                0.2499958334027766,
                0.28333333333333327,
                0.4000066667777796,
                58.31569473842103,
                5.98135840794e-05
            ],
            "Id": "cpuUtilization",
            "Label": "myRequestLabel"
        }
    ]
}

I'm trying to transform it to a table that looks like this:

Timestamp | Value | Id
2021-12-28T02:11:00Z | 5.0 | groupDesiredCapacity

I googled around and came up with this jq query .MetricDataResults[] | {t: .Timestamps[], v: .Values[],i: .Id}|[.t,.v,.i]|@csv but it seems to "multiply" the values instead of mapping them.

A bit stuck on this one, any ideas/help are appreciated.

like image 973
GreenOak Avatar asked Dec 30 '22 11:12

GreenOak


2 Answers

Use the transpose builtin to match the array members to each other

jq -r '.MetricDataResults[] | ([.Timestamps, .Values] | transpose[]) + [.Id] | @csv'
"2021-12-28T02:11:00Z",5,"groupDesiredCapacity"
"2021-12-28T02:10:00Z",2,"groupDesiredCapacity"
"2021-12-28T02:09:00Z",2,"groupDesiredCapacity"
...
"2021-12-28T01:54:00Z",0.4000066667777796,"cpuUtilization"
"2021-12-28T01:53:00Z",58.31569473842103,"cpuUtilization"
"2021-12-28T01:52:00Z",5.98135840794e-05,"cpuUtilization"

Demo

If you want to have your desired output format instead of csv, replace @csv with join(" | ")

jq -r '.MetricDataResults[] | ([.Timestamps, .Values] | transpose[]) + [.Id] | join(" | ")'
2021-12-28T02:11:00Z | 5 | groupDesiredCapacity
2021-12-28T02:10:00Z | 2 | groupDesiredCapacity
2021-12-28T02:09:00Z | 2 | groupDesiredCapacity
...
2021-12-28T01:54:00Z | 0.4000066667777796 | cpuUtilization
2021-12-28T01:53:00Z | 58.31569473842103 | cpuUtilization
2021-12-28T01:52:00Z | 5.98135840794e-05 | cpuUtilization

Demo

like image 144
pmf Avatar answered Jan 01 '23 23:01

pmf


A straightforward alternative:

.MetricDataResults[]
| .Id as $Id
| range(0; .Timestamps|length) as $i
| [.Timestamps[$i], .Values[$i], $Id]
| @csv
like image 33
peak Avatar answered Jan 02 '23 00:01

peak