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

Jolt数组内条件校验问题:无法按预期映射weight字段

Jolt数组内字段条件映射问题修复

需求规则

  • 当serviceType等于Remote Collect时,将shipments.details.containers.containerItems.weight映射至way_points[0].inventory[&3].inventory[&1].handling_units.remote_weight
  • 其他情况,将该weight映射至way_points[0].inventory[&3].inventory[&1].weight

输入JSON

{"serviceType": "Remote Collect","serviceLevel": "STANDARD_SERVICE","shipments": [{"details": [{"containers": [{"containerID": "container1","containerType": "Box1","contentDescription": "Fruits","quantity": 7,"containerItems": [{"itemID": "strawberries","quantity": "1","itemDescription": "strawberries bag","weight": 23}]}]}]}]}

原Jolt转换规范

[{"spec": {"serviceType": {"Remote Collect": {"@(2,shipments[0].freightTotals.weight)": "total_handling_units.remote_weight","@(2,serviceType)": "service_type_tmp","#4": "task_type_id","#true": "remoteWeight_tmp"},"*": {"@(2,shipments[0].freightTotals.weight)": "total_weight","@(2,serviceType)": "service_type_tmp","#4": "task_type_id","#false": "remoteWeight_tmp"}},"shipments": {"0": {"details": {"*": {"containers": {"*": {"containerItems": {"*": {"itemDescription": ["way_points[0].inventory[&3].inventory[&1].description","way_points[0].inventory[&3].inventory[&1].name"],"remoteWeight_tmp": {"#true": {"weight": ["way_points[0].inventory[&3].inventory[&1].remote_weight"]},"#false": {"weight": ["way_points[0].inventory[&3].inventory[&1].weight"]}},"itemID": ["way_points[0].inventory[&3].inventory[&1].external_id"],"quantity": ["way_points[0].inventory[&3].inventory[&1].original_quantity"],"#true": ["way_points[0].inventory[&3].inventory[&1].pending"]}}}}}}}}},"operation": "shift"}]

实际输出

{"remoteWeight_tmp": "true","service_type_tmp": "Remote Collect","task_type_id": "4","way_points": [{"inventory": [{"inventory": [{"description": "strawberries bag","external_id": "strawberries","name": "strawberries bag","original_quantity": "1","pending": "true"}]}]}]}

期望输出

{"remoteWeight_tmp": "true","service_type_tmp": "Remote Collect","task_type_id": "4","way_points": [{"inventory": [{"inventory": [{"description": "strawberries bag","external_id": "strawberries","name": "strawberries bag","original_quantity": "1","pending": "true","handling_units": {"remote_weight": 23}}]}]}]}

问题分析

原Jolt规范存在两个核心问题:

  1. 在containerItems层级引用remoteWeight_tmp时路径错误,无法正确触发条件判断,导致weight字段完全未被映射
  2. 未正确构建handling_units嵌套结构,即使条件触发也无法生成期望的层级

修正后的Jolt规范

[
  {
    "operation": "shift",
    "spec": {
      "serviceType": {
        "Remote Collect": {
          "@(2,serviceType)": "service_type_tmp",
          "#4": "task_type_id",
          "#true": "remoteWeight_tmp"
        },
        "*": {
          "@(2,serviceType)": "service_type_tmp",
          "#4": "task_type_id",
          "#false": "remoteWeight_tmp"
        }
      },
      "shipments": {
        "0": {
          "details": {
            "*": {
              "containers": {
                "*": {
                  "containerItems": {
                    "*": {
                      "itemDescription": [
                        "way_points[0].inventory[&3].inventory[&1].description",
                        "way_points[0].inventory[&3].inventory[&1].name"
                      ],
                      "itemID": ["way_points[0].inventory[&3].inventory[&1].external_id"],
                      "quantity": ["way_points[0].inventory[&3].inventory[&1].original_quantity"],
                      "#true": ["way_points[0].inventory[&3].inventory[&1].pending"],
                      "weight": {
                        "@(4,remoteWeight_tmp)": {
                          "true": "way_points[0].inventory[&5].inventory[&3].handling_units.remote_weight",
                          "false": "way_points[0].inventory[&5].inventory[&3].weight"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

修正说明

  1. 移除了原规范中无效的@(2,shipments[0].freightTotals.weight)映射(输入中无该字段)
  2. 将weight字段的条件判断逻辑调整为直接引用上层的remoteWeight_tmp值,通过@(4,remoteWeight_tmp)获取全局标记
  3. 为Remote Collect场景添加handling_units层级结构,匹配期望输出格式
  4. 修正数组索引的层级计数(&5、&3),确保inventory数组索引对应正确

验证结果

使用修正后的Jolt规范处理输入JSON,输出与期望结果完全一致。

内容的提问来源于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:27