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

如何用Jolt将嵌套JSON转换为指定结构的扁平JSON?

Jolt转换规则方案

场景1:合并shipmentItems为扁平Items列表

输入示例

{
  "order": {
    "orderId": "ORD-001",
    "orderName": "Test Order",
    "orderDate": "2024-05-20",
    "grandTotal": 150.50,
    "otherField": "无需保留"
  },
  "shipments": [
    {
      "shipmentId": "SHP-001",
      "shipmentItems": [
        {
          "facilityId": "FAC-001",
          "facilityExternalId": "EXT-FAC-001",
          "productId": "PROD-001",
          "otherItemField": "无需保留"
        },
        {
          "facilityId": "FAC-002",
          "facilityExternalId": "EXT-FAC-002",
          "productId": "PROD-002"
        }
      ]
    },
    {
      "shipmentId": "SHP-002",
      "shipmentItems": [
        {
          "facilityId": "FAC-003",
          "facilityExternalId": "EXT-FAC-003",
          "productId": "PROD-003"
        }
      ]
    }
  ]
}

Jolt转换规则

[
  {
    "operation": "shift",
    "spec": {
      "order": {
        "orderId": "orderId",
        "orderName": "orderName",
        "orderDate": "orderDate",
        "grandTotal": "grandTotal"
      },
      "shipments": {
        "*": {
          "shipmentItems": {
            "*": {
              "facilityId": "Items[&1].facilityId",
              "facilityExternalId": "Items[&1].facilityExternalId",
              "productId": "Items[&1].productId"
            }
          }
        }
      }
    }
  }
]

输出结果

{
  "orderId": "ORD-001",
  "orderName": "Test Order",
  "orderDate": "2024-05-20",
  "grandTotal": 150.50,
  "Items": [
    {
      "facilityId": "FAC-001",
      "facilityExternalId": "EXT-FAC-001",
      "productId": "PROD-001"
    },
    {
      "facilityId": "FAC-002",
      "facilityExternalId": "EXT-FAC-002",
      "productId": "PROD-002"
    },
    {
      "facilityId": "FAC-003",
      "facilityExternalId": "EXT-FAC-003",
      "productId": "PROD-003"
    }
  ]
}

场景2:新增提取指定IdentificationTypeId的identificationValue字段

输入示例(包含Identifications数组)

{
  "order": {
    "orderId": "ORD-001",
    "orderName": "Test Order",
    "orderDate": "2024-05-20",
    "grandTotal": 150.50
  },
  "shipments": [
    {
      "shipmentId": "SHP-001",
      "shipmentItems": [
        {
          "facilityId": "FAC-001",
          "facilityExternalId": "EXT-FAC-001",
          "productId": "PROD-001",
          "Identifications": [
            {
              "IdentificationTypeId": "SKU",
              "idValue": "SKU-001"
            },
            {
              "IdentificationTypeId": "EXT_PROD_ID",
              "idValue": "EXT-PROD-001"
            }
          ]
        },
        {
          "facilityId": "FAC-002",
          "facilityExternalId": "EXT-FAC-002",
          "productId": "PROD-002",
          "Identifications": [
            {
              "IdentificationTypeId": "EXT_PROD_ID",
              "idValue": "EXT-PROD-002"
            }
          ]
        }
      ]
    }
  ]
}

Jolt转换规则

注意:将规则中的EXT_PROD_ID替换为你实际需要匹配的IdentificationTypeId值

[
  {
    "operation": "shift",
    "spec": {
      "order": {
        "orderId": "orderId",
        "orderName": "orderName",
        "orderDate": "orderDate",
        "grandTotal": "grandTotal"
      },
      "shipments": {
        "*": {
          "shipmentItems": {
            "*": {
              "facilityId": "Items[&1].facilityId",
              "facilityExternalId": "Items[&1].facilityExternalId",
              "productId": "Items[&1].productId",
              "Identifications": {
                "*": {
                  "IdentificationTypeId": {
                    "EXT_PROD_ID": {
                      "@(1,idValue)": "Items[&3].identificationValue"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

输出结果

{
  "orderId": "ORD-001",
  "orderName": "Test Order",
  "orderDate": "2024-05-20",
  "grandTotal": 150.50,
  "Items": [
    {
      "facilityId": "FAC-001",
      "facilityExternalId": "EXT-FAC-001",
      "productId": "PROD-001",
      "identificationValue": "EXT-PROD-001"
    },
    {
      "facilityId": "FAC-002",
      "facilityExternalId": "EXT-FAC-002",
      "productId": "PROD-002",
      "identificationValue": "EXT-PROD-002"
    }
  ]
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 21:55:16