Jolt数组内条件判断实现求助:按serviceType赋值weight字段
Jolt数组元素条件赋值解决方案
针对需求:当根节点的serviceType等于"Remote Collect"时,将shipments.details.containers.containerItems.weight替换为waypoint.inventory.inventory.handling_units.remote_weight,否则替换为waypoint.inventory.inventory.weight,以下是可行的Jolt转换规范:
[ { "operation": "shift", "spec": { "*": "&", "shipments": { "*": { "details": { "*": { "containers": { "*": { "containerID": "&4.&3.&2.&1.containerID", "containerType": "&4.&3.&2.&1.containerType", "contentDescription": "&4.&3.&2.&1.contentDescription", "quantity": "&4.&3.&2.&1.quantity", "containerItems": { "*": { "itemID": "&6.&5.&4.&3.&2.&1.itemID", "quantity": "&6.&5.&4.&3.&2.&1.quantity", "itemDescription": "&6.&5.&4.&3.&2.&1.itemDescription", "@(5,waypoint.inventory.inventory.handling_units.remote_weight)": { "@(6,serviceType)": { "Remote Collect": { "#": "&7.&6.&5.&4.&3.&2.weight" } } }, "@(5,waypoint.inventory.inventory.weight)": { "@(6,serviceType)": { "!Remote Collect": { "#": "&7.&6.&5.&4.&3.&2.weight" } } }, "@(5,waypoint.inventory.inventory.weight)": { "*": { "#": "&7.&6.&5.&4.&3.&2.weight" } } } } } } } } } } } } ]
关键逻辑说明
- 保留原有字段:
"*": "&"确保除需要修改的weight外,其他所有字段原样保留。 - 数组遍历:用
*遍历shipments、details、containers、containerItems下的每一个数组元素,保证所有层级的元素都能被处理。 - 条件判断:
- 通过
@(6,serviceType)引用根节点的serviceType字段,结合键匹配实现条件分支。 - 当
serviceType为"Remote Collect"时,将remote_weight赋值给weight;否则赋值普通weight。 - 最后通配符
*分支处理未知的serviceType值,默认使用普通weight。
- 通过
注意事项
输入JSON需要包含waypoint节点(示例中未给出,假设结构如下),否则无法正确获取权重值:
{ "waypoint": { "inventory": { "inventory": { "weight": 10, "handling_units": { "remote_weight": 15 } } } }, // 其他原有字段... }
内容的提问来源于stack exchange,提问作者rashmi n
相关产品推荐
相关产品推荐

