JOLT转换嵌套数组与重复键:如何将记录展开为独立对象
JOLT JSON转换规则修正
需求说明
需将包含readings数组的JSON转换为每条记录独立的对象,每个对象包含meter、Ah、epoch_timestamp、copper_id(对应原uniqueID)字段,忽略原始的timestamp字段。现有shift操作将同一仪表的字段合并为数组,不符合需求。
原始JSON
{"readings":[{"meter":"02319771","records":[{"timestamp":"28/06/2024 14:35:00","values":{"Ah":4350.947,"epoch_timestamp":1719578227,"uniqueID":"12005"}},{"timestamp":"28/06/2024 15:00:00","values":{"Ah":4353.141,"epoch_timestamp":1719579724,"uniqueID":"12005"}}]}]}
现有JOLT规范
[{"operation":"shift","spec":{"readings":{"*":{"meter":"[&1].meter_serial","records":{"*":{"values":{"epoch_timestamp":"[&4].&","uniqueID":"[&4].copper_id","Ah":"[&4].ah"}}}}}}]
当前转换结果
[{"meter_serial":"02319771","epoch_timestamp":[1719578227,1719579724],"copper_id":["12005","12005"],"ah":[4350.947,4353.141]}]
期望输出
[{"meter":"02319771","Ah":4350.947,"epoch_timestamp":1719578227,"copper_id":"12005"},{"meter":"02319771","Ah":4353.141,"epoch_timestamp":1719579724,"copper_id":"12005"}]
修正后的JOLT规范
[ { "operation": "shift", "spec": { "readings": { "*": { "records": { "*": { "values": { "Ah": "[&3].[&1].Ah", "epoch_timestamp": "[&3].[&1].epoch_timestamp", "uniqueID": "[&3].[&1].copper_id" }, "@(2,meter)": "[&3].[&1].meter" } } } } } }, { "operation": "shift", "spec": { "*": { "*": "[]" } } } ]
规则解释
- 第一个shift操作:
- 用
[&3].[&1]定位:&3对应外层readings的索引,&1对应当前record的索引,确保每条record生成独立的嵌套对象 - 通过
@(2,meter)将当前仪表的meter字段传递到对应record的对象中 - 映射
values下的字段到目标字段,将uniqueID重命名为copper_id
- 用
- 第二个shift操作:
- 将嵌套的二维数组扁平化,得到一维对象数组
内容的提问来源于stack exchange,提问作者Tokolosk
相关产品推荐
相关产品推荐

