Gouri Mahapatra
Gouri Mahapatra

Reputation: 139

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

Upvotes: 1

Views: 637

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65105

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

Upvotes: 1

Related Questions