如何在现有JOLT脚本中为输出JSON添加head和foot属性?
问题
现有如下JSON输入,需要修改现有JOLT脚本,将输入中第一个对象的head和foot属性添加到输出的每一个JSON对象中,请问如何实现?
输入JSON
[ { "PropertyName": "KER ZONE 1", "head": "13084", "foot": "48b4e" }, { "Properties": [ { "PropertyName": "KER ZONE 1", "Property": [ { "StartTime": "2023-04-03", "Data": [ { "GPSY": "45.28", "GPSX": "-3.24", "Val": "11.0", "Metric": "mvw" }, { "GPSY": "45.28", "GPSX": "-3.24", "Val": "2231", "Metric": "Hum" }, { "GPSY": "45.28", "GPSX": "-3.24", "Val": "0.00", "Metric": "wat" }, { "GPSY": "45.28", "GPSX": "-3.24", "Val": "58.82", "Metric": "temp" }, { "GPSY": "33.27", "GPSX": "-4.29", "Val": "0.00", "Metric": "wat" }, { "GPSY": "33.27", "GPSX": "-4.29", "Val": "9.8", "Metric": "mvw" }, { "GPSY": "33.27", "GPSX": "-4.29", "Val": "2206", "Metric": "Hum" }, { "GPSY": "33.27", "GPSX": "-4.29", "Val": "58.28", "Metric": "temp" } ] } ] } ], "CallStatus": "1" } ]
期望输出JSON
[ { "Hum": "2231", "Road_1": "SD", "Road_2": "SF", "Temp": 14.9, "Temp_F": 58.82, "Time": "2023-04-03", "X": "-3.24", "Y": "45.28", "mvw": 11, "head": "13084", "foot": "48b4e" }, { "Hum": "2206", "Road_1": "SD", "Road_2": "SF", "Temp": 14.6, "Temp_F": 58.28, "Time": "2023-04-03", "X": "-4.29", "Y": "33.27", "mvw": 9.8, "head": "13084", "foot": "48b4e" } ]
现有JOLT脚本
[ { "operation": "shift", "spec": { "*": { "*": { "*": { "*": { "*": { "*": { "@2,StartTime": "@1,GPSY.@1,GPSX.Time", "GPS*": "@(1,GPSY).@(1,GPSX).&", "@Val": "@(1,GPSY).@(1,GPSX).@Metric" } } } } } } } }, { "operation": "modify-overwrite-beta", "spec": { "*": { "*": { "Temp_C": "=doubleSum(@(1,temp),-32)", "Temp": "=divideAndRound(3,@(1,Temp_C),1.8)", "temp": "=toDouble", "mvw": "=toDouble", "hum": "=toInteger" } } } }, { //硬编码值 "operation": "default", "spec": { "*": { "*": { "Road_1": "SD", "Road_2": "SF" } } } }, { "operation": "shift", "spec": { "*": { "*": { "*": "&2.&3.&", "GPSY": "&2.&3.Y", "GPSX": "&2.&3.X", "mvw": "&2.&3.mvw", "hum": "&2.&3.Hum", "temp": "&2.&3.Temp_F" } } } }, { "operation": "cardinality", "spec": { "*": { "*": { "*": "ONE" } } } }, { "operation": "shift", "spec": { "*": { "*": "[]" } } }, { "operation": "remove", "spec": { "*": { "wat": "", "Temp_C": "" } } } ]
解决方案
要把输入中第一个对象的head和foot属性添加到输出的每个对象中,需要在现有JOLT脚本中新增提取、保留、注入这三个关键步骤,修改后的完整JOLT脚本如下:
[ { "operation": "shift", "spec": { "0": { // 提取第一个对象中的head和foot,存入meta节点 "head": "meta.head", "foot": "meta.foot" }, "1": { // 保留原有数据处理逻辑 "*": { "*": { "*": { "*": { "*": { "@2,StartTime": "@1,GPSY.@1,GPSX.Time", "GPS*": "@(1,GPSY).@(1,GPSX).&", "@Val": "@(1,GPSY).@(1,GPSX).@Metric" } } } } } } } }, { "operation": "modify-overwrite-beta", "spec": { "*": { "*": { "Temp_C": "=doubleSum(@(1,temp),-32)", "Temp": "=divideAndRound(3,@(1,Temp_C),1.8)", "temp": "=toDouble", "mvw": "=toDouble", "hum": "=toInteger" } } } }, { //硬编码值 "operation": "default", "spec": { "*": { "*": { "Road_1": "SD", "Road_2": "SF" } } } }, { "operation": "shift", "spec": { "*": { "*": { "*": "&2.&3.&", "GPSY": "&2.&3.Y", "GPSX": "&2.&3.X", "mvw": "&2.&3.mvw", "hum": "&2.&3.Hum", "temp": "&2.&3.Temp_F" } }, "meta": { // 保留meta节点中的head和foot "*": "&" } } }, { "operation": "cardinality", "spec": { "*": { "*": { "*": "ONE" } }, "head": "ONE", "foot": "ONE" } }, { "operation": "shift", "spec": { "*": { "*": { "@(2,head)": "&2.&3.head", // 将head注入每个结果对象 "@(2,foot)": "&2.&3.foot", // 将foot注入每个结果对象 "*": "&2.&3.&" } } } }, { "operation": "shift", "spec": { "*": { "*": "[]" } } }, { "operation": "remove", "spec": { "*": { "wat": "", "Temp_C": "" } } } ]
核心修改说明
- 第一步shift操作:新增对输入数组第一个元素(索引0)的处理,将
head和foot提取到meta节点单独存储,同时保留原有对第二个元素的数据处理逻辑。 - 第四步shift操作:添加对
meta节点的保留规则,确保head和foot不会被后续操作丢弃。 - 新增第六步shift操作:在数据转换为数组前,通过
@(2,head)向上两层引用保存的head值,将head和foot批量注入到每个结果对象中。 - 第五步cardinality操作:新增对
head和foot的唯一性约束,避免重复值干扰。
内容的提问来源于stack exchange,提问作者casper
相关产品推荐
相关产品推荐

