[英]How to perform group-by over JSON data using JSONATA
I have a JSON structure given below.我有一个下面给出的 JSON 结构。 I want to perform a couple of transformations on this data.我想对这些数据执行几个转换。 I came to know about JSONATA and it looks fantastic to me, but since I am new, I am struggling a little bit around transformation.我开始了解 JSONATA,它对我来说看起来很棒,但由于我是新手,我在转型方面有些挣扎。 I want to calculate the total of count (sum) for each module date for the given date.我想计算给定日期的每个模块日期的总计数(总和)。 The expected result is given below.预期结果如下。
Input:输入:
{
"id": "6332acbfe13e6063dcb740ef",
"record": [
{
"date": "2022-09-22",
"entries": [
{
"repo": "SRE-MAC-PDM-TEAM",
"prod": [
{
"name": "modules/dynatrace",
"count": 16
}
]
},
{
"repo": "SRE-MAC-IBO-BONUS-PAYMENTS-REPO",
"prod": [
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 3
}
]
},
{
"repo": "SRE-MAC-ANAPostPurchase",
"prod": [
{
"name": "modules/dynatrace",
"count": 17
}
]
},
{
"repo": "SRE-MAC-ANA-SubscriptionsRewards",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 8
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 8
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 4
}
]
},
{
"repo": "SRE-MAC-CSP-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 9
}
]
},
{
"repo": "SRE-MAC-IBOIP-REPO",
"prod": [
{
"name": "modules/dynatrace",
"count": 6
}
]
},
{
"repo": "SRE-MAC-IBOLIFE-REPO",
"preprod": [
{
"name": "modules/dynatrace",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 2
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 3
}
],
"prod": [
{
"name": "modules/dynatrace",
"count": 5
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
]
},
{
"repo": "SRE-MAC-PL-APPLICATION",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 4
}
],
"prod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 2
}
]
},
{
"repo": "SRE-MAC-TPS-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/dynatrace/calculated_service_metric",
"count": 1
},
{
"name": "modules/dynatrace/anomalies/custom/metric_id",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 11
}
]
}
]
},
{
"date": "2022-09-19",
"entries": [
{
"repo": "SRE-MAC-PDM-TEAM",
"prod": [
{
"name": "modules/dynatrace",
"count": 14
}
]
},
{
"repo": "SRE-MAC-IBO-BONUS-PAYMENTS-REPO",
"prod": [
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 3
}
]
},
{
"repo": "SRE-MAC-ANAPostPurchase",
"prod": [
{
"name": "modules/dynatrace",
"count": 15
}
]
},
{
"repo": "SRE-MAC-ANA-SubscriptionsRewards",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 5
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 5
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 3
}
]
},
{
"repo": "SRE-MAC-CSP-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 6
}
]
},
{
"repo": "SRE-MAC-IBOIP-REPO",
"prod": [
{
"name": "modules/dynatrace",
"count": 6
}
]
},
{
"repo": "SRE-MAC-IBOLIFE-REPO",
"preprod": [
{
"name": "modules/dynatrace",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 2
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 3
}
],
"prod": [
{
"name": "modules/dynatrace",
"count": 4
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
]
},
{
"repo": "SRE-MAC-PL-APPLICATION",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 3
}
],
"prod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 2
}
]
},
{
"repo": "SRE-MAC-TPS-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/dynatrace/calculated_service_metric",
"count": 1
},
{
"name": "modules/dynatrace/anomalies/custom/metric_id",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 9
}
]
}
]
},
{
"date": "2022-09-12",
"entries": [
{
"repo": "SRE-MAC-PDM-TEAM",
"prod": [
{
"name": "modules/dynatrace",
"count": 4
}
]
},
{
"repo": "SRE-MAC-IBO-BONUS-PAYMENTS-REPO",
"prod": [
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 2
}
]
},
{
"repo": "SRE-MAC-ANAPostPurchase",
"prod": [
{
"name": "modules/dynatrace",
"count": 3
}
]
},
{
"repo": "SRE-MAC-ANA-SubscriptionsRewards",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 3
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 4
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 2
}
]
},
{
"repo": "SRE-MAC-CSP-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 4
}
]
},
{
"repo": "SRE-MAC-IBOIP-REPO",
"prod": [
{
"name": "modules/dynatrace",
"count": 3
}
]
},
{
"repo": "SRE-MAC-IBOLIFE-REPO",
"preprod": [
{
"name": "modules/dynatrace",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 2
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 2
}
],
"prod": [
{
"name": "modules/dynatrace",
"count": 3
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
]
},
{
"repo": "SRE-MAC-PL-APPLICATION",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 2
}
],
"prod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 2
}
]
},
{
"repo": "SRE-MAC-TPS-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/dynatrace/calculated_service_metric",
"count": 1
},
{
"name": "modules/dynatrace/anomalies/custom/metric_id",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 7
}
]
}
]
},
{
"date": "2022-09-05",
"entries": [
{
"repo": "SRE-MAC-PDM-TEAM",
"prod": [
{
"name": "modules/dynatrace",
"count": 12
}
]
},
{
"repo": "SRE-MAC-IBO-BONUS-PAYMENTS-REPO",
"prod": [
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 1
}
]
},
{
"repo": "SRE-MAC-ANAPostPurchase",
"prod": [
{
"name": "modules/dynatrace",
"count": 11
}
]
},
{
"repo": "SRE-MAC-ANA-SubscriptionsRewards",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 2
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 5
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/monitors/http-monitors/basic",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 1
}
]
},
{
"repo": "SRE-MAC-CSP-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 5
}
]
},
{
"repo": "SRE-MAC-IBOIP-REPO",
"prod": [
{
"name": "modules/dynatrace",
"count": 3
}
]
},
{
"repo": "SRE-MAC-IBOLIFE-REPO",
"preprod": [
{
"name": "modules/dynatrace",
"count": 1
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
],
"prod": [
{
"name": "modules/dynatrace",
"count": 2
},
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
]
},
{
"repo": "SRE-MAC-PL-APPLICATION",
"preprod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
],
"prod": [
{
"name": "modules/splunk/hec-token",
"count": 1
},
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
]
},
{
"repo": "SRE-MAC-TPS-APPLICATION",
"preprod": [
{
"name": "modules/aws/lambda/logs_streaming_splunk",
"count": 1
}
],
"prod": [
{
"name": "modules/dynatrace/alerting_profiles",
"count": 1
},
{
"name": "modules/dynatrace/notifications/splunk-on-call",
"count": 1
},
{
"name": "modules/dynatrace/calculated_service_metric",
"count": 1
},
{
"name": "modules/dynatrace/anomalies/custom/metric_id",
"count": 1
},
{
"name": "modules/dynatrace",
"count": 2
}
]
}
]
}
],
"metadata": {
"name": "",
"readCountRemaining": 98,
"timeToExpire": 86389,
"createdAt": "2022-09-27T07:56:47.003Z"
}
}
Expected Output:预计 Output:
[
"2022-09-22" : [
{
"name" : "modules/dynatrace",
"count": 11
},
{
"name" : "modules/dynatrace",
"count": 4
},
{
"name" : "modules/splunk/hec-token",
"count": 14
}
....
],
"2022-09-19" : [
{
"name" : "modules/dynatrace",
"count": 52
},
.....
]
.
.
.
]
This should do the grouping for you:这应该为你做分组:
(
record{
date: $reduce(entries.prod.name, function($acc, $item) {
$merge([$acc, {
$item: $count(entries.prod.name[$=$item])
}])
}, {}) ~> $each(function($value, $key) {
{ "name": $key, "count": $value }
})
}
)
You can check it out live here: https://stedi.link/LiOttdB您可以在这里现场查看: https://stedi.link/LiOttdB
UPDATE - even shorter solution:更新 - 更短的解决方案:
(
record{
date: entries.*{ $.name: $sum($.count) }
~> $each(function($c, $n) {{ "name": $n, "count": $c }})
}
)
Thanks @mralex to answer my question.感谢@mralex 回答我的问题。 But unfortunately it is not working in older version which is getting used in the latest JSON-API Grafana module.但不幸的是,它在最新的 JSON-API Grafana 模块中使用的旧版本中不起作用。 So I myself create another solution which is given below:所以我自己创建了另一个解决方案,如下所示:
(
record.(
$t1 := entries.preprod{ $.name: $sum($.count) };
$t2 := entries.prod{ $.name: $sum($.count) };
$t3 := $distinct($append($t1.$keys(),$t2.$keys()));
$t3.(
$t4 := ($exists($lookup($t1, $)) ? $lookup($t1, $) : 0) + ($exists($lookup($t2, $)) ? $lookup($t2, $): 0);
{
'name': $,
'count' : $t4
}
)
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.