I have a json format with metrics & timestamps & values from AWS Cloudwatch.
{
"Messages": [],
"MetricDataResults": [
{
"Timestamps": [
"2021-07-07T13:26:00Z"
],
"StatusCode": "Complete",
"Values": [
0.0
],
"Id": "m19",
"Label": "CPUSurplusCreditsCharged"
},
{
"Timestamps": [
"2021-07-07T13:28:00Z",
"2021-07-07T13:27:00Z",
"2021-07-07T13:26:00Z",
"2021-07-07T13:25:00Z",
"2021-07-07T13:24:00Z",
"2021-07-07T13:23:00Z"
],
"StatusCode": "Complete",
"Values": [
12.750425014167137,
13.033116114731422,
12.70812153130781,
12.975,
15.441924032067199,
12.916451392476791
],
"Id": "m20",
"Label": "CPUUtilization"
},
{
"Timestamps": [
"2021-07-07T13:29:00Z",
"2021-07-07T13:28:00Z",
"2021-07-07T13:27:00Z",
"2021-07-07T13:26:00Z",
"2021-07-07T13:25:00Z",
"2021-07-07T13:24:00Z",
"2021-07-07T13:23:00Z"
],
"StatusCode": "Complete",
"Values": [
0.7,
0.6999533364442371,
0.6998833527745376,
0.6999416715273727,
0.7,
0.7001166861143524,
0.6998950157476379
],
"Id": "m21",
"Label": "NetworkReceiveThroughput"
}
]
}
I put these values in an array variable using the jq command.
And the result is output to the array variable as follows.
jq -r '.MetricDataResults[] | "\(.Label) \(.Timestamps) \(.Values)"' test.json | while read Label timestamp value
do
Label=`echo $Label | sed 's/\"//g; s/\[//g; s/\]//g; s/,/ /g'`
timestamp=`echo $timestamp | sed 's/\"//g; s/\[//g; s/\]//g; s/,/ /g'`
value=`echo $value | sed 's/\"//g; s/\[//g; s/\]//g; s/,/ /g'`
arr_timestamp=($timestamp)
arr_value=($value)
echo $Label
echo ${arr_timestamp[@]}
echo ${arr_value[@]}
done
Evictions
2021-07-07T10:51:00Z 2021-07-07T10:50:00Z 2021-07-07T10:49:00Z 2021-07-07T10:48:00Z 2021-07-07T10:47:00Z 2021-07-07T10:46:00Z 2021-07-07T10:45:00Z
0 0 0 0 0 0 0
CPUUtilization
2021-07-07T10:50:00Z 2021-07-07T10:49:00Z 2021-07-07T10:48:00Z 2021-07-07T10:47:00Z 2021-07-07T10:46:00Z 2021-07-07T10:45:00Z
1.5333333333333332 1.4666666666666666 1.5833333333333333 1.5333333333333332 1.4916666666666665 1.4916666666666665
IsMaster
2021-07-07T10:51:00Z 2021-07-07T10:50:00Z 2021-07-07T10:49:00Z 2021-07-07T10:48:00Z 2021-07-07T10:47:00Z 2021-07-07T10:46:00Z 2021-07-07T10:45:00Z
1 1 1 1 1 1 1
When timestamp is not the same length for each array variable,
I want to display only values in the same timestamp as a single string.
For example
"2021-07-07T10:51:00Z Evictions = 0\nIsMaster = 1"
"2021-07-07T10:50:00Z Evictions = 0\nCPUUtilization = 1.5333333333333332\n IsMaster = 1"
...
My head is bad and I can't think of a good way.
Please let me know if there is any good way.
I don't have much time so please help on stackoverflow.
- Add
I mean group by Timestamps. Like this
{
"MetricDataResults": [
{
"Timestamps": "2021-07-07T13:28:00Z",
"Label" : [
"CPUUtilization",
"NetworkReceiveThroughput"
],
"Values" : [
12.750425014167137,
0.7
]
},
{
"Timestamps": "2021-07-07T13:27:00Z",
"Label" : [
"CPUUtilization",
"NetworkReceiveThroughput"
],
"Values" : [
13.033116114731422,
0.6999533364442371
]
},
{
"Timestamps": "2021-07-07T13:26:00Z",
"Label" : [
"CPUUtilization",
"NetworkReceiveThroughput",
"CPUSurplusCreditsCharged"
],
"Values" : [
12.70812153130781,
0.6998833527745376,
0.0
]
}
]
}