使用Jolt转换复杂嵌套JSON未达预期,请求专家协助
Jolt转换嵌套JSON问题排查与解决方案
输入JSON
[ { "metadata": { "event_id": "0635FCAD8AEA1EEDB6868EEFA4D25D34", "event_key": "OutboundScheduling_0DE1_040D", "event_name": "OutboundScheduling_Update", "event_source": "RS4_003", "event_time": "2023-04-11T06:45:37.4124Z", "event_version": "1.0" }, "payload": { "departuresite": "0DE1", "shippingpoint": "040D", "leadtimes": { "transportationlt": [ { "destinationsite": "0DE2", "destinationlt": [ { "shippingcondition": "01", "destinationsitelt": 2 }, { "shippingcondition": "03", "destinationsitelt": 2 }, { "shippingcondition": "GR", "destinationsitelt": 2 }, { "shippingcondition": "OP", "destinationsitelt": 2 }, { "shippingcondition": "OS", "destinationsitelt": 2 }, { "shippingcondition": "OX", "destinationsitelt": 2 }, { "shippingcondition": "OY", "destinationsitelt": 2 } ] } ], "dncreationleadtimes": [ { "distributionchannel": "70", "ordertype": "ZU25", "orderreason": "DRP", "dncreationlt": 1 } ], "dnprocessingleadtimes": [ { "distributionchannel": "70", "ordertype": "ZU25", "orderreason": "DRP", "dnprocessinglt": 1 } ], "dnloadingleadtimes": [ { "distributionchannel": "70", "shippingcondition": "OU", "ordertype": "ZU25", "orderreason": "DRP", "dnloadinglt": 1 } ], "vasleadtimes": [] } } } ]
预期输出JSON
[ { "departuresite": "0DE1", "shippingpoint": "040D", "destinationsite": "0DE2", "distributionchannel": "70", "ordertype": "ZU25", "orderreason": "DRP", "dncreationlt": 1, "dnloadinglt": 1 } ]
当前使用的Jolt转换规则
[ { "operation": "shift", "spec": { "*": { "payload": { "leadtimes": { "transportationlt": { "*": { "destinationlt": { "*": { "@5,departuresite": "&3[#2].departuresite", "@5,shippingpoint": "&3[#2].shippingpoint", "@2,destinationsite": "&3[#2].destinationsite", "@4,dncreationleadtimes[&].distributionchannel": "&3[#2].distributionchannel", "@4,dncreationleadtimes[&].ordertype": "&3[#2].ordertype", "@4,dncreationleadtimes[&].orderreason": "&3[#2].orderreason", "@4,dncreationleadtimes[&].dncreationlt": "&3[#2].dncreationlt", "@4,dnloadingleadtimes[&].dnloadinglt": "&3[#2].dnloadinglt" } } } } } } } } }, { // get rid of the object keys generated within the previous spec "operation": "shift", "spec": { "*": { "*": "" } } } ]
问题分析
- 数组索引引用错误:原规则中
@4,dncreationleadtimes[&].xxx里的&对应destinationlt数组的循环索引(0-6),但dncreationleadtimes仅含1个索引为0的元素,导致无法匹配目标字段。 - 不必要的数组遍历:遍历
destinationlt的7个元素会生成7条重复数据,与预期单条结果不符。 - 层级定位偏差:部分上层字段的相对路径计算不准确,导致字段映射失败。
正确转换规则
[ { "operation": "shift", "spec": { "*": { "payload": { "departuresite": "[0].departuresite", "shippingpoint": "[0].shippingpoint", "leadtimes": { "transportationlt": { "0": { "destinationsite": "[0].destinationsite" } }, "dncreationleadtimes": { "0": { "distributionchannel": "[0].distributionchannel", "ordertype": "[0].ordertype", "orderreason": "[0].orderreason", "dncreationlt": "[0].dncreationlt" } }, "dnloadingleadtimes": { "0": { "dnloadinglt": "[0].dnloadinglt" } } } } } } } ]
规则说明
- 直接指定数组索引
0提取各数组的唯一元素,避免无效遍历。 - 统一将所有字段映射到
[0]节点下,确保输出为单条数据的数组结构。 - 层级定位清晰,直接从对应节点提取所需字段,规避相对路径引用错误。
内容的提问来源于stack exchange,提问作者Thiyagaraj Narayanan
相关产品推荐
相关产品推荐

