arraysjsoncopytransformationjolt

Copying certain fields to every array object on a JSON Transformation with JOLT


I'm struggling with transforming a JSON with JOLT.

I have the following input JSON:

{
    "AAA": {
        "Product":"Product A",
        "Owner": {
            "Name": "NAME SURNAME",
            "Age": "20",
            "ID": [
                {
                    "IDType": "CPF",
                    "IDNumber": "0000000000000"
                }
            ],
            "Contact": {
                "Email": "test@test.com",
                "Phone": "1111111111",
                "Add": {
                    "Add1": "aaaa",
                    "Add2": "bbbb",
                    "City": "city"
                }
            }
        }
    },
    "BBB": [
        {
            "Field 1": "F1",
            "Field 2": "F2",
            "Field 3": {
                "F31": "F3.1",
                "F32": "F3.2"
            }
        },
        {
            "Field 1": "F1",
            "Field 2": "F2",
            "Field 3": {
                "F31": "F3.1",
                "F32": "F3.2"
            }
        },
        {
            "Field 1": "F1",
            "Field 2": "F2",
            "Field 3": {
                "F31": "F3.1",
                "F32": "F3.2"
            }
        }
    ]
}

and i need to copy fields from "AAA.Owner" and put it into a new object (New Field) in every "Field 3" in the "BBB" Array.

The output JSON would look like this:

{
    "AAA": {
        "Product":"Product A",
        "Owner": {
            "Name": "NAME SURNAME",
            "Age": "20",
            "ID": [
                {
                    "IDType": "CPF",
                    "IDNumber": "0000000000000"
                }
            ],
            "Contact": {
                "Email": "test@test.com",
                "Phone": "1111111111",
                "Add": {
                    "Add1": "aaaa",
                    "Add2": "bbbb",
                    "City": "city"
                }
            }
        }
    },
    "BBB": [
        {
            "Field 1": "F1",
            "Field 2": "F2",
            "Field 3": {
                "F31": "F3.1",
                "F32": "F3.2",
                "New Field": {
                    "Name": "NAME SURNAME",
                    "Id": {
                        "IDType": "CPF",
                        "IDNumber": "0000000000000"
                    },
                    "Contact": {
                        "Email": "test@test.com",
                        "Phone": "1111111111",
                        "Add": {
                            "Add1": "aaaa",
                            "Add2": "bbbb",
                            "City": "city"
                        }
                    }
                }
            }
        },
        {
            "Field 1": "F1",
            "Field 2": "F2",
            "Field 3": {
                "F31": "F3.1",
                "F32": "F3.2",
                "New Field": {
                    "Name": "NAME SURNAME",
                    "Id": {
                        "IDType": "CPF",
                        "IDNumber": "0000000000000"
                    },
                    "Contact": {
                        "Email": "teste@teste.com",
                        "Phone": "1111111111",
                        "Add": {
                            "Add1": "aaaa",
                            "Add2": "bbbb",
                            "City": "city"
                        }
                    }
                }
            }
        },
        {
            "Field 1": "F1",
            "Field 2": "F2",
            "Field 3": {
                "F31": "F3.1",
                "F32": "F3.2",
                "New Field": {
                    "Name": "NAME SURNAME",
                    "Id": {
                        "IDType": "LALALA",
                        "IDNumber": "0000000000000"
                    },
                    "Contact": {
                        "Email": "test@test.com",
                        "Phone": "1111111111",
                        "Add": {
                            "Add1": "aaaa",
                            "Add2": "bbbb",
                            "City": "city"
                        }
                    }
                }
            }
        }
    ]
}

Could someone help me with this? Thanks!

I cannot manage to iterate over the items in BBB and insert the object without screwing everything up..


Solution

  • Can you try the below spec:

    [
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "BBB": {
            "*": {
              "*": "&2[&1].&",
              "Field 3": {
                "*": "&3[&2].&1.&",
                "@(3,AAA.Owner)": "&3[&2].&1.NewField"
              }
            }
          }
        }
      }
    ]
    

    If you dont want to copy all the fields form AAA.Owner, then you have to the clean up that first then use shift above to get the final transformation.