Search code examples
jsonjolt

Jolt transform a json with array of child elements with array of parent element


I am trying to transform a json which has array element in the child. I want to convert it to json array of parent elements with parent attribs in all json elements

My input json

{
    "Transmission": {
        "TransmissionGUID": "transId1",
        "Employer": [
            {
                "EmployerPartyID": "employerId1",
                "Employee": [
                    {
                        "EmployeeGenderCode": "M",
                        "MaritalStatusCode": "Married"

                    },
                    {
                        "EmployeeGenderCode": "F",
                        "MaritalStatusCode": "UnMarried"

                    }
                ]
            },
            {
                "EmployerPartyID": "employerId2",
                "Employee": [
                    {
                        "EmployeeGenderCode": "M2",
                        "MaritalStatusCode": "Married2"

                    },
                    {
                        "EmployeeGenderCode": "F2",
                        "MaritalStatusCode": "UnMarried"

                    }
                ]
            }
        ]
    }
}

I want to transform this to

[
    {
        "Transmission": {
            "TransmissionGUID": "transId1",
            "Employer": [
                {
                    "EmployerPartyID": "employerId1",
                    "Employee": [
                        {
                            "EmployeeGenderCode": "M",
                            "MaritalStatusCode": "Married"
                        }
                    ]
                }
            ]
        }
    },
     {
        "Transmission": {
            "TransmissionGUID": "transId1",
            "Employer": [
                {
                    "EmployerPartyID": "employerId1",
                    "Employee": [
                        {
                            "EmployeeGenderCode": "F",
                            "MaritalStatusCode": "UnMarried"
                        }
                    ]
                }
            ]
        }
    },
    {
        "Transmission": {
            "TransmissionGUID": "transId1",
            "Employer": [
                {
                    "EmployerPartyID": "employerId2",
                    "Employee": [
                        {
                            "EmployeeGenderCode": "M2",
                            "MaritalStatusCode": "Married2"
                        }
                    ]
                }
            ]
        }
    },
    {
        "Transmission": {
            "TransmissionGUID": "transId1",
            "Employer": [
                {
                    "EmployerPartyID": "employerId",
                    "Employee": [
                        {
                            "EmployeeGenderCode": "F2",
                            "MaritalStatusCode": "UnMarried2"
                        }
                    ]
                }
            ]
        }
    }
]

I tried writing the jolt spec for the transformation as below

[
    {
      "operation": "shift",
      "spec": {
        "Transmission": {
    
          "Employer": {
            "*": {
     
              "Employee": {
                "*": {
                    "@(4,TransmissionGUID)": "[].Transmission.TransmissionGUID",
                    "@(2,EmployerPartyID)": "[].Transmission.Employer[].EmployerPartyID",
                  "@": "[].Transmission.Employer[].Employee[]"
                  
                  
                }
              }
            }
          }
        }
      }
    }
  ]

But its not giving desired output. Can anyone help me with this?


Solution

  • I dont think you can do this transformation using one shift spec. I'm able to do it using two shifts where the first shift groups parent-child information under unique key. The unique key is created using combination of child index and parent index from the parent\child arrays. for example the first Employer (index 0) has two children so if you concatenate parent index and child index using underscore (_) in the middle you will get a unique key for each parent child as 0_0 & 0_1. For second employer children it will 1_0 & 1_1 and so on. Once you have grouped each parent-child under its unique key, the second shift transformation will basically grab each parent-child and dump it in an array without the unique key and that should give you the needed result.

    [
      {
        "operation": "shift",
        "spec": {
          "Transmission": {
            "Employer": {
              "*": {
                "Employee": {
                  "*": {
                    "@(4,TransmissionGUID)": "&3_&1.Transmission.TransmissionGUID",
                    "@(2,EmployerPartyID)": "&3_&1.Transmission.Employer[#].EmployerPartyID",
                    "@": "&3_&1.Transmission.Employer[#].Employee[]"
                  }
                }
              }
            }
          }
        }
        },
      {
        "operation": "shift",
        "spec": {
          "*": "[]"
        }
      }
     ]