如何使用JOLT按站点统计JSON记录数并生成指定汇总结果
需求说明
需基于给定的JSON文件,按站点生成汇总信息。
输入JSON
[ { "time": "2024-03-25T00:00:00Z", "station": "1", "temp": "2.0", "rain": "1.6" }, { "time": "2024-03-25T01:00:00Z", "station": "1", "temp": "2.6", "rain": "2.1" }, { "time": "2024-03-25T01:00:00Z", "station": "2", "temp": "2.6", "rain": "2.1" }, { "time": "2024-03-25T01:00:00Z", "station": "3", "temp": "2.6", "rain": "2.1" } ]
期望输出
[ { "sid": "1", "cnt": 2 }, { "sid": "2", "cnt": 1 }, { "sid": "3", "cnt": 1 } ]
当前问题
参考相关内容编写了如下JOLT转换代码:
[ { "operation": "shift", "spec": { "*": { "station": { "@(2,[&1])": "@(1).[]" } } } }, { "operation": "modify-overwrite-beta", "spec": { "*": { "*": { "cnt": "=size(@(2))", "id": "=toInteger" } } } }, { "operation": "shift", "spec": { "*": { "*": "" } } } ]
但输出结果不符合预期,实际输出为:
[ { "time": "2024-03-25T00:00:00Z", "station": "1", "temp": "2.0", "rain": "1.6", "cnt": 2 }, { "time": "2024-03-25T01:00:00Z", "station": "1", "temp": "2.6", "rain": "2.1", "cnt": 2 }, { "time": "2024-03-25T01:00:00Z", "station": "2", "temp": "2.6", "rain": "2.1", "cnt": 1 }, { "time": "2024-03-25T01:00:00Z", "station": "3", "temp": "2.6", "rain": "2.1", "cnt": 1 } ]
请问如何通过JOLT实现符合期望的转换?
内容的提问来源于stack exchange,提问作者casper
相关产品推荐
相关产品推荐

