You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 22:02:13