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

求助:使用Jolt Transform展平复杂嵌套JSON

Jolt Transform 实现嵌套JSON展开与字段重命名

问题描述

需要将嵌套结构的JSON转换为扁平化数组,每个component元素作为独立对象,同时满足:

  • 继承顶层(guid、WebHookId、siteLocation)及ReferenceCountry数组内的RefCountry、Country字段
  • 继承对应WTGLevel下的所有字段
  • 将component内的Material、Quantity、UnitOfMeasure字段添加Cmpt_前缀,保留CapacityType字段
  • 支持多SCID及每个SCID下多组件的场景

输入JSON

{
  "guid": "String 1",
  "WebHookId": 1234567890,
  "siteLocation": "offshore",
  "ReferenceCountry": [
    {
      "RefCountry": "DE",
      "Country": "NL"
    }
  ],
  "WTGLevel": [
    {
      "SCID": "5gmqATexwYw",
      "ReferenceCountry": "DE",
      "Material": "WTG_01",
      "Description": "CS-048948A-#01",
      "Quantity": 1,
      "UnitOfMeasure": "EA",
      "WTGType": "V136-4.0MW",
      "RotorDiameter": "136 m",
      "MarkRelease": "3F",
      "NominalEffect": 4200,
      "HubHeight": 112,
      "component": [
        {
          "Material": "NACELLE_01",
          "Description": "Nacelle MSI",
          "Quantity": 1,
          "UnitOfMeasure": "EA",
          "CapacityType": "N_V136_4.0MW_TR_3F",
          "MaterialGroup": 325,
          "DerivedMaterial": 29227496,
          "MaterialIndicator": "PLAN_LO",
          "PriceBaseID": "P/NAC:P_3MW/MK_3F/R_136M/FRQ_50HZ/LOC_04"
        },
        {
          "Material": "SP_AVI_LAMP_01",
          "Description": "Site Parts Aviation Lamp MSI",
          "Quantity": 1,
          "UnitOfMeasure": "EA",
          "CapacityType": "NAC_OPT",
          "MaterialGroup": 5,
          "DerivedMaterial": 29197892,
          "MaterialIndicator": "VAR",
          "PriceBaseID": "P/AV_LA:AVWTG_3MW/AV_LA_18/AV_Q_1"
        }
      ]
    },
    {
      "SCID": "zyUL6lemDdM",
      "ReferenceCountry": "DE",
      "Material": "WTG_01",
      "Description": "CS-042692A-#01",
      "Quantity": 1,
      "UnitOfMeasure": "EA",
      "WTGType": "V136-4.0MW",
      "RotorDiameter": "136 m",
      "MarkRelease": "3E",
      "NominalEffect": 4.2,
      "HubHeight": 112,
      "component": [
        {
          "Material": "BLADE_01",
          "Description": "Blade MSI",
          "Quantity": 3,
          "UnitOfMeasure": "EA",
          "CapacityType": "BLA_67M_STR",
          "MaterialGroup": 40,
          "DerivedMaterial": 29156738,
          "MaterialIndicator": "VAR",
          "PriceBaseID": "P/BLA:P_3MW/MK_3E/R_136M/DEICE_N/BLATY_00"
        },
        {
          "Material": "DRIVE_TRAIN_01",
          "Description": "Drive Train MSI",
          "Quantity": 1,
          "UnitOfMeasure": "EA",
          "CapacityType": "DT_V136_4.0ME_3E",
          "MaterialGroup": 540,
          "DerivedMaterial": 29219265,
          "MaterialIndicator": "VAR",
          "PriceBaseID": "P/DT:P_3MW/MK_3E/R_136M/LOC_01"
        }
      ]
    }
  ]
}

期望输出JSON

[
  {
    "guid": "String 1",
    "WebHookId": 1234567890,
    "siteLocation": "offshore",
    "RefCountry": "DE",
    "Country": "NL",
    "SCID": "5gmqATexwYw",
    "ReferenceCountry": "DE",
    "Material": "WTG_01",
    "Description": "CS-048948A-#01",
    "Quantity": 1,
    "UnitOfMeasure": "EA",
    "WTGType": "V136-4.0MW",
    "RotorDiameter": "136 m",
    "MarkRelease": "3F",
    "NominalEffect": 4200,
    "HubHeight": 112,
    "Cmpt_Material": "NACELLE_01",
    "Cmpt_Quantity": 1,
    "Cmpt_UnitOfMeasure": "EA",
    "CapacityType": "N_V136_4.0MW_TR_3F"
  },
  {
    "guid": "String 1",
    "WebHookId": 1234567890,
    "siteLocation": "offshore",
    "RefCountry": "DE",
    "Country": "NL",
    "SCID": "5gmqATexwYw",
    "ReferenceCountry": "DE",
    "Material": "WTG_01",
    "Description": "CS-048948A-#01",
    "Quantity": 1,
    "UnitOfMeasure": "EA",
    "WTGType": "V136-4.0MW",
    "RotorDiameter": "136 m",
    "MarkRelease": "3F",
    "NominalEffect": 4200,
    "HubHeight": 112,
    "Cmpt_Material": "SP_AVI_LAMP_01",
    "Cmpt_Quantity": 1,
    "Cmpt_UnitOfMeasure": "EA",
    "CapacityType": "NAC_OPT"
  },
  {
    "guid": "String 1",
    "WebHookId": 1234567890,
    "siteLocation": "offshore",
    "RefCountry": "DE",
    "Country": "NL",
    "SCID": "zyUL6lemDdM",
    "ReferenceCountry": "DE",
    "Material": "WTG_01",
    "Description": "CS-042692A-#01",
    "Quantity": 1,
    "UnitOfMeasure": "EA",
    "WTGType": "V136-4.0MW",
    "RotorDiameter": "136 m",
    "MarkRelease": "3E",
    "NominalEffect": 4.2,
    "HubHeight": 112,
    "Cmpt_Material": "BLADE_01",
    "Cmpt_Quantity": 3,
    "Cmpt_UnitOfMeasure": "EA",
    "CapacityType": "BLA_67M_STR"
  },
  {
    "guid": "String 1",
    "WebHookId": 1234567890,
    "siteLocation": "offshore",
    "RefCountry": "DE",
    "Country": "NL",
    "SCID": "zyUL6lemDdM",
    "ReferenceCountry": "DE",
    "Material": "WTG_01",
    "Description": "CS-042692A-#01",
    "Quantity": 1,
    "UnitOfMeasure": "EA",
    "WTGType": "V136-4.0MW",
    "RotorDiameter": "136 m",
    "MarkRelease": "3E",
    "NominalEffect": 4.2,
    "HubHeight": 112,
    "Cmpt_Material": "DRIVE_TRAIN_01",
    "Cmpt_Quantity": 1,
    "Cmpt_UnitOfMeasure": "EA",
    "CapacityType": "DT_V136_4.0ME_3E"
  }
]

Jolt 转换规范

[
  {
    "operation": "shift",
    "spec": {
      // 传递顶层字段到每个结果对象
      "guid": "[&1].guid",
      "WebHookId": "[&1].WebHookId",
      "siteLocation": "[&1].siteLocation",
      // 提取ReferenceCountry数组内的字段
      "ReferenceCountry": {
        "*": {
          "RefCountry": "[&3].RefCountry",
          "Country": "[&3].Country"
        }
      },
      // 遍历WTGLevel数组
      "WTGLevel": {
        "*": {
          // 传递当前WTGLevel下的所有非component字段
          "*": "[&2].&",
          // 遍历当前WTGLevel下的component数组
          "component": {
            "*": {
              // 重命名component内的指定字段,添加Cmpt_前缀
              "Material": "[&4].Cmpt_Material",
              "Quantity": "[&4].Cmpt_Quantity",
              "UnitOfMeasure": "[&4].Cmpt_UnitOfMeasure",
              "CapacityType": "[&4].CapacityType"
            }
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      // 移除结果对象中多余的component字段
      "*": {
        "component": ""
      }
    }
  }
]

规范说明

  1. Shift 操作:
    • 顶层字段直接映射到结果数组的每个对象
    • 通过[&3]层级定位,将ReferenceCountry数组内的字段传递到每个结果对象
    • 用*遍历WTGLevel数组,将当前层级的非component字段全部映射到结果对象
    • 遍历每个WTGLevel下的component数组,通过[&4]层级定位到结果数组的对应位置,完成指定字段的重命名
  2. Remove 操作:清理结果对象中多余的component字段,保证输出结构整洁

该规范支持任意数量的SCID及每个SCID下任意数量的component元素,会自动将每个component展开为独立对象,并继承所有上层所需字段。

内容的提问来源于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.22 19:07:00