I want to monitor highest CpuUsage from this result. How do I parse a json
that changes its category on a time base?
{
"MonitorHistory": "{\"2021-10-06T00:00:00Z\":{\"CpuUsage\":\"0.45\"},\"2021-10-06T00:01:00Z\":{\"CpuUsage\":\"0.57\"},\"2021-10-06T00:02:00Z\":{\"CpuUsage\":\"0.73\"},\"2021-10-06T00:03:00Z\":{\"CpuUsage\":\"0.6\"},\"2021-10-06T00:04:00Z\":{\"CpuUsage\":\"0.45\"},\"2021-10-06T00:05:00Z\":{\"CpuUsage\":\"0.57\"},\"2021-10-06T00:06:00Z\":{\"CpuUsage\":\"0.6\"},\"2021-10-06T00:07:00Z\":{\"CpuUsage\":\"0.65\"},\"2021-10-06T00:08:00Z\":{\"CpuUsage\":\"0.72\"},\"2021-10-06T00:09:00Z\":{\"CpuUsage\":\"0.57\"},\"2021-10-06T00:10:00Z\":{\"CpuUsage\":\"0.87\"},\"2021-10-06T00:11:00Z\":{\"CpuUsage\":\"0.67\"},\"2021-10-06T00:12:00Z\":{\"CpuUsage\":\"0.78\"},\"2021-10-06T00:13:00Z\":{\"CpuUsage\":\"0.72\"},\"2021-10-06T00:14:00Z\":{\"CpuUsage\":\"0.73\"},\"2021-10-06T00:15:00Z\":{\"CpuUsage\":\"0.92\"},\"2021-10-06T00:16:00Z\":{\"CpuUsage\":\"1.0\"},\"2021-10-06T00:17:00Z\":{\"CpuUsage\":\"1.23\"},\"2021-10-06T00:18:00Z\":{\"CpuUsage\":\"0.93\"},\"2021-10-06T00:19:00Z\":{\"CpuUsage\":\"0.98\"},\"2021-10-06T00:20:00Z\":{\"CpuUsage\":\"0.87\"},\"2021-10-06T00:21:00Z\":{\"CpuUsage\":\"0.95\"},\"2021-10-06T00:22:00Z\":{\"CpuUsage\":\"1.18\"},\"2021-10-06T00:23:00Z\":{\"CpuUsage\":\"1.08\"},\"2021-10-06T00:24:00Z\":{\"CpuUsage\":\"1.15\"},\"2021-10-06T00:25:00Z\":{\"CpuUsage\":\"1.37\"},\"2021-10-06T00:26:00Z\":{\"CpuUsage\":\"1.72\"},\"2021-10-06T00:27:00Z\":{\"CpuUsage\":\"1.6\"},\"2021-10-06T00:28:00Z\":{\"CpuUsage\":\"1.47\"}}"
}
My desired result is a collection of CpuUsage values like
0.45 0.73 0.6...
CodePudding user response:
You'll need to first parse the MonitorHistory
json string to get it into a workable form. Then take advantage of the fact that json objects when enumerated in jq are in alphabetical order (since they appear to be ISO timestamps). Just pull the values from each of the entries. Since it's all just single property objects, just spit out the values.
.MonitorHistory | fromjson[][] | tonumber
CodePudding user response:
Use max_by
to find the maximum according to a specific path.
If you want to retain the timestamp, convert the objects using to_entries
to a key
/value
pair and find the maximum:
jq '.MonitorHistory | fromjson | to_entries | max_by(.value.CpuUsage | tonumber) | {(.key):.value}'
Otherwise, if you only need the highest number
jq '[.MonitorHistory | fromjson[].CpuUsage | tonumber] | max'