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

求助:使用Jolt转换扁平化复杂嵌套货运JSON

修正Jolt Spec实现货运JSON扁平化输出

输入JSON示例

{
  "shipment": {
    "id": "SHP-001",
    "carrier": "XYZ Logistics",
    "consignments": [
      {
        "id": "CON-001",
        "shipper": "ABC Corp",
        "packages": [
          {
            "id": "PKG-001",
            "weight": 15.5,
            "dimensions": {
              "length": 30,
              "width": 20,
              "height": 15
            },
            "trackingNumber": "TRK-12345"
          },
          {
            "id": "PKG-002",
            "weight": 8.2,
            "dimensions": {
              "length": 25,
              "width": 15,
              "height": 10
            },
            "trackingNumber": "TRK-67890"
          }
        ]
      }
    ]
  }
}

期望输出数组

[
  {
    "shipmentId": "SHP-001",
    "carrier": "XYZ Logistics",
    "consignmentId": "CON-001",
    "shipper": "ABC Corp",
    "packageId": "PKG-001",
    "weight": 15.5,
    "length": 30,
    "width": 20,
    "height": 15,
    "trackingNumber": "TRK-12345"
  },
  {
    "shipmentId": "SHP-001",
    "carrier": "XYZ Logistics",
    "consignmentId": "CON-001",
    "shipper": "ABC Corp",
    "packageId": "PKG-002",
    "weight": 8.2,
    "length": 25,
    "width": 15,
    "height": 10,
    "trackingNumber": "TRK-67890"
  }
]

原失败Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "shipment": {
        "id": "shipmentId",
        "carrier": "carrier",
        "consignments": {
          "*": {
            "id": "consignmentId",
            "shipper": "shipper",
            "packages": {
              "*": {
                "id": "packageId",
                "weight": "weight",
                "dimensions": {
                  "length": "length",
                  "width": "width",
                  "height": "height"
                },
                "trackingNumber": "trackingNumber"
              }
            }
          }
        }
      }
    }
  }
]

修正后的Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "shipment": {
        "id": "[&1].shipmentId",
        "carrier": "[&1].carrier",
        "consignments": {
          "*": {
            "id": "[&2].[&1].consignmentId",
            "shipper": "[&2].[&1].shipper",
            "packages": {
              "*": {
                "id": "[&3].[&2].[&1].packageId",
                "weight": "[&3].[&2].[&1].weight",
                "dimensions": {
                  "length": "[&4].[&3].[&2].[&1].length",
                  "width": "[&4].[&3].[&2].[&1].width",
                  "height": "[&4].[&3].[&2].[&1].height"
                },
                "trackingNumber": "[&3].[&2].[&1].trackingNumber"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "flatten",
    "spec": {
      "*": {
        "*": {
          "*": "[]"
        }
      }
    }
  }
]

关键修正点

  • 原Spec未使用数组索引占位符(&n)关联层级关系,导致嵌套数组的属性无法正确映射到同一对象。修正后的shift操作通过[&n]定位不同层级的数组索引,确保每个包裹属性与所属运单、托运单一一绑定。
  • 新增flatten操作将嵌套的三维数组结构直接展开为一维数组,完全匹配预期的扁平化输出格式。

内容的提问来源于stack exchange,提问作者Thiyagaraj Narayanan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 09:52:43