使用 JOLT 转换将样本 JSON 转换为嵌套的 JSON 数组

Convert sample JSON to nested JSON array using JOLT Transformation

我遇到了一个问题,使用 jolt 转换将平面 JSON 转换为嵌套 JSON。我对 jolt Transformation 很陌生。输入和输出详细信息如下。

我的输入:

[
  {
    "policyNo": 1,
    "lProdCode": 500,
    "name": "Prasad",
    "id": "10",
    "Age": "56"
  },
  {
    "policyNo": 1,
    "lProdCode": 500,
    "name": "Mahapatra",
    "id": "101",
    "Age": "56"
  },
  {
    "policyNo": 2,
    "lProdCode": 500,
    "name": "Pra",
    "id": "109",
    "Age": "56"
  },
  {
    "policyNo": 3,
    "lProdCode": 400,
    "name": "Pra",
    "id": "108",
    "Age": "56"
  },
  {
    "policyNo": 1,
    "lProdCode": 500,
    "name": "Pra",
    "id": "108",
    "Age": "56"
  }
]

预期输出

[
  {
    "policyNo": 1,
    "lProdCode": 500,
    "beneficiaries": [
      {
        "name": "Prasad",
        "id": "10900629001",
        "Age": "56"
      },
      {
        "name": "Mahapatra",
        "id": "10900629001",
        "Age": "56"
      },
      {
        "name": "Pra",
        "id": "108",
        "Age": "56"
      }
    ]
  },
  {
    "policyNo": 2,
    "lProdCode": 500,
    "beneficiaries": [
      {
        "name": "Pra",
        "id": "10900629001",
        "Age": "56"
      }
    ]
  },
  {
    "policyNo": 3,
    "lProdCode": 400,
    "beneficiaries": [
      {
        "name": "Pra",
        "id": "108",
        "Age": "56"
      }
    ]
  }
]

原则上,您需要按 policyNo 属性进行分组,同时为 policyNo&lProdCode 以外的属性生成一个新列表 (beneficiaries)。这可能会在 shift 转换中处理。然后再添加三个步骤来修剪来自第一个转换的粗糙度,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "policyNo": "@(1,policyNo).&",
        "lProdCode": "@(1,policyNo).&",
        "*": "@(1,policyNo).beneficiaries[&1].&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "policyNo": "ONE",
        "lProdCode": "ONE"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]