下面给出了一个JSON结构。我想对这些数据执行几个转换。我逐渐了解了JSONATA,这在我看来是非常棒的,但是由于我是新来的,所以我有点为改变而挣扎。我要计算给定日期的每个模块日期的计数(和)总数。预期结果如下所示。
输入:
{
"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"
}
}预期产出:
[
"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
},
.....
]
.
.
.
]发布于 2022-09-28 12:09:29
这将为您进行分组:
(
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 }
})
}
)你可以在这里现场查看:https://stedi.link/LiOttdB
更新-更短的解决方案:
(
record{
date: entries.*{ $.name: $sum($.count) }
~> $each(function($c, $n) {{ "name": $n, "count": $c }})
}
)发布于 2022-09-29 08:23:09
谢谢@mralex回答我的问题。但不幸的是,它不能在旧版本中工作,该版本正在最新的JSON模块中使用。因此,我自己创造了另一个解决方案,如下所示:
(
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
}
)
)
) https://stackoverflow.com/questions/73880731
复制相似问题