简体   繁体   中英

Convert sample JSON to nested JSON array using JOLT Transformation

I am facing a problem, transforming flat JSON to the nested JSON using jolt transformation. And I am very new to jolt Transformation. Input and output detail is given below.

My input:

[
  {
    "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"
  }
]

expected output

[
  {
    "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"
      }
    ]
  }
]

Principally you need to group by policyNo attribute along with generating a new list( beneficiaries ) for the attributes other than policyNo & lProdCode . That might be handled within a shift transformation. Then add three more steps to prune the roughnesses stems from the first transformation such as

[
  {
    "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": {
      "*": ""
    }
  }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM