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

使用JOLT将前缀冗余JSON数组转为嵌套对象并去重

JOLT转换:合并同一客户的License与Connector到数组中

我有一组带前缀冗余的JSON数组,需要通过JOLT转换为嵌套结构并去除重复的客户对象。之前使用@Barbaros提供的转换规则后,出现了重复的Customer对象,每个重复对象对应不同的License和Connector信息。我希望将同一Customer下的所有License和Connector整合到Licenses数组中,相关输入、现有规则、当前结果及期望结果如下:

输入JSON

[
  {
    "License_idlicense": 1,
    "License_StartDate": "2022-11-15 00:00:00.0",
    "License_EndDate": "2022-11-29 00:00:00.0",
    "License_MonthlySearchMax": 500,
    "CustomerId": 0,
    "Guid": "c24c1fa3-0388-4c08-b431-8d0f05fe263a",
    "Name": "Usuário Trial",
    "CustStartDate": "2022-11-15 00:00:00.0",
    "Connector_ConnectorId": 0,
    "Connector_Name": "BigDataCorp",
    "Connector_Version": "1.01"
  },
  {
    "License_idlicense": 2,
    "License_StartDate": "2022-11-15 00:00:00.0",
    "License_EndDate": "2022-11-30 00:00:00.0",
    "License_MonthlySearchMax": 500,
    "CustomerId": 0,
    "Guid": "c24c1fa3-0388-4c08-b431-8d0f05fe263a",
    "Name": "Usuário Trial",
    "CustStartDate": "2022-11-15 00:00:00.0",
    "Connector_ConnectorId": 1,
    "Connector_Name": "Credilink",
    "Connector_Version": "1.01"
  }
]

现有JOLT转换规则

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "CustomerId": "ECOLicense.Customers[&1].&",
        "Guid": "ECOLicense.Customers[&1].&",
        "Name": "ECOLicense.Customers[&1].&",
        "*_*": "ECOLicense.Customers[&1].&(0,1).&(0,2)"
      }
    }
  }
]

当前转换结果

{
  "ECOLicense" : {
    "Customers" : [ {
      "CustomerId" : 0,
      "Guid" : "c24c1fa3-0388-4c08-b431-8d0f05fe263a",
      "Name" : "Usuário Trial",
      "License" : {
        "idlicense" : 1,
        "StartDate" : "2022-11-15 00:00:00.0",
        "EndDate" : "2022-11-29 00:00:00.0",
        "MonthlySearchMax" : 500
      },
      "Connector" : {
        "ConnectorId" : 0,
        "Name" : "BigDataCorp",
        "Version" : "1.01"
      }
    }, {
      "CustomerId" : 0,
      "Guid" : "c24c1fa3-0388-4c08-b431-8d0f05fe263a",
      "Name" : "Usuário Trial",
      "License" : {
        "idlicense" : 2,
        "StartDate" : "2022-11-15 00:00:00.0",
        "EndDate" : "2022-11-30 00:00:00.0",
        "MonthlySearchMax" : 500
      },
      "Connector" : {
        "ConnectorId" : 1,
        "Name" : "Credilink",
        "Version" : "1.01"
      }
    } ]
  }
}

期望结果

{
  "ECOLicense": {
    "Customers": [
      {
        "CustomerId": 0,
        "Guid": "c24c1fa3-0388-4c08-b431-8d0f05fe263a",
        "Name": "Usuário Trial",
        "Licenses": [
          {
            "License": {
              "idlicense": 1,
              "StartDate": "2022-11-15 00:00:00.0",
              "EndDate": "2022-11-29 00:00:00.0",
              "MonthlySearchMax": 500
            },
            "Connector": {
              "ConnectorId": 0,
              "Name": "BigDataCorp",
              "Version": "1.01"
            }
          },
          {
            "License": {
              "idlicense": 2,
              "StartDate": "2022-11-15 00:00:00.0",
              "EndDate": "2022-11-30 00:00:00.0",
              "MonthlySearchMax": 500
            },
            "Connector": {
              "ConnectorId": 1,
              "Name": "Credilink",
              "Version": "1.01"
            }
          }
        ]
      }
    ]
  }
}

修正后的JOLT转换规则

[
  // 按CustomerId分组,聚合同一客户的所有License和Connector到Licenses数组
  {
    "operation": "shift",
    "spec": {
      "*": {
        "CustomerId": "temp.@(1,CustomerId).CustomerId",
        "Guid": "temp.@(1,CustomerId).Guid",
        "Name": "temp.@(1,CustomerId).Name",
        "License_*": "temp.@(1,CustomerId).Licenses[&1].License.&",
        "Connector_*": "temp.@(1,CustomerId).Licenses[&1].Connector.&"
      }
    }
  },
  // 将分组后的结构转换为目标格式的Customers数组
  {
    "operation": "shift",
    "spec": {
      "temp": {
        "*": {
          "*": "ECOLicense.Customers[#2].&",
          "Licenses": "ECOLicense.Customers[#2].&"
        }
      }
    }
  }
]

规则说明

  1. 第一个shift操作:
    • 以CustomerId作为分组键,将同一客户的所有信息聚合到同一个临时对象中
    • 把每个条目中的License和Connector字段分别映射到对应客户的Licenses数组元素里
  2. 第二个shift操作:
    • 将临时分组的对象转换为Customers数组结构,确保每个客户只出现一次
    • 保留客户基本信息和整合后的Licenses数组

内容的提问来源于stack exchange,提问作者Filipe Miguel Ferreira

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 10:15:43