Search code examples
jsontransformationjolttransformer-model

JOLT Transformation - Data grouping based on child array objects field


I received a json format and wanted to transform it in a format that will be easier to display in the UI whereby the grouping will be based on levelOne and levelTwo fields, which are inside the child array object. I have tried as much as i can but nothing came close to the desired output I wanted.

Input:

{
  "id": "101",
  "myLevel": "Beginner",
  "riskProducts": [
    {
      "id": "1",
      "product": {
        "id": "2",
        "levelOne": "Equities",
        "levelTwo": "Global Developed Large Cap Equity",
        "productName": "Allianz Thematica Fund",
        "proposedAmountToInvest": "null",
        "proposedHoldings": "2",
        "unitPrice": "111.44"
      }
    },
    {
      "id": "2",
      "product": {
        "id": "1",
        "levelOne": "Equities",
        "levelTwo": "Global Developed Large Cap Equity",
        "productName": "AB Low Volatility Equity Portfolio",
        "proposedAmountToInvest": "null",
        "proposedHoldings": "2",
        "unitPrice": "150.18"
      }
    },
    {
      "id": "4",
      "product": {
        "id": "4",
        "levelOne": "Equities",
        "levelTwo": "US Large Cap Equity",
        "productName": "Franklin Technology Fund",
        "proposedAmountToInvest": "null",
        "proposedHoldings": "4",
        "unitPrice": "93.02"
      }
    },
    {
      "id": "5",
      "product": {
        "id": "5",
        "levelOne": "Fixed Income",
        "levelTwo": "Global Aggregate Bonds",
        "productName": "PIMCO GIS Income Fund HKD Inc",
        "proposedAmountToInvest": "null",
        "proposedHoldings": "7",
        "unitPrice": "11.93"
      }
    }
  ]
}

Desired Output:

{
  "myLevel": "Beginner",
  "levelOne": [
    {
      "id": "Equities",
      "levelTwo": [
        {
          "id": "Global Developed Large Cap Equity",
          "product": [
            {
              "id": "2",
              "productName": "Allianz Thematica Fund",
              "proposedHoldings": "2",
              "unitPrice": "111.44"
            },
            {
              "id": "1",
              "productName": "AB Low Volatility Equity Portfolio",
              "proposedHoldings": "2",
              "unitPrice": "150.18"
            }
          ],
          "sPercentage": "0",
          "tPercentage": "0"
        },
        {
          "id": "US Large Cap Equity",
          "product": [
            {
              "id": "4",
              "productName": "Franklin Technology Fund",
              "proposedHoldings": "4",
              "unitPrice": "93.02"
            }
          ],
          "sPercentage": "0",
          "tPercentage": "0"
        }
      ]
    },
    {
      "id": "Fixed Income",
      "levelTwo": [
        {
          "id": "Global Aggregate Bonds",
          "product": [
            {
              "id": "5",
              "productName": "PIMCO GIS Income Fund HKD Inc",
              "proposedAmountToInvest": "null",
              "proposedHoldings": "7",
              "unitPrice": "11.93"
            }
          ],
          "sPercentage": "0",
          "tPercentage": "0"
        }
      ]
    }
  ]
}

Appreciate the help and please do recommend a tutorial site where I would be able to understand better how the transformation works.


Solution

  • You can use this spec:

    [
      {
        "operation": "shift",
        "spec": {
          "myLevel": "&",
          "riskProducts": {
            "*": {
              "product": {
                "*": "levelOne.@(1,levelOne).levelTwo.@(1,levelTwo).&1[&2].&",
                "level*": "should_be_remove",
                "proposedAmountToInvest": "should_be_remove"
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": "=recursivelySquashNulls"
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "sPercentage": "0",
                  "tPercentage": "0"
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "myLevel": "&",
          "level*": {
            "*": {
              "$": "&2[#2].id",
              "level*": {
                "*": {
                  "$": "&4.[#4].&2[#2].id",
                  "*": "&4.[#4].&2[#2].&"
                }
              }
            }
          }
        }
      }
    ]