Search code examples
jsonapache-nifijolt

Flatten the json when there is nested array and where is no nested array


Dynamic jolt to handle when single element or multiple elements in a array

My jolt spec is working perfectly fine if we receive an array in PartnerFunction Nested array , but if they send a JSON involving only one element in PartnerFunction, its failing

Input 1

{
  "CustomerMaster": {
    "Rootnode": {
      "KUNNR": "123",
      "NAME1": "XXXX",
      "LAND1": "SE",
      "SalesArea": {
        "VKORG": "1301",
        "VTWEG": "10",
        "SPART": "00",
        "AUFSD": "",
        "FAKSD": "",
        "LIFSD": "",
        "PartnerFunction": [
          {
            "PARVW": "RE",
            "PARZA": "000",
            "KUNN2": "1231"
          },
          {
            "PARVW": "RE",
            "PARZA": "000",
            "KUNN2": "1232"
          }
        ]
      }
    }
  }
}

Input 2

{
  "CustomerMaster": {
    "Rootnode": {
      "KUNNR": "123",
      "NAME1": "XXXX",
      "LAND1": "SE",
      "SalesArea": {
        "VKORG": "1301",
        "VTWEG": "10",
        "SPART": "00",
        "AUFSD": "",
        "FAKSD": "",
        "LIFSD": "",
        "PartnerFunction": {
          "PARVW": "RE",
          "PARZA": "000",
          "KUNN2": "1231"
        }
      }
    }
  }
}

Jolt Spec i m using

[
  {
    "operation": "shift",
    "spec": {
      "CustomerMaster": {
        "Rootnode": {
          "SalesArea": {
            "PartnerFunction": {
              "*": {
                "@(3,KUNNR)": "[&1].KUNNR",
                "@(3,NAME1)": "[&1].NAME1",
                "@(3,NAME2)": "[&1].NAME2",
                "@(3,NAME3)": "[&1].NAME3",
                "@(3,NAME4)": "[&1].NAME4",
                "@(3,LAND1)": "[&1].LAND1",
                "@(2,VKORG)": "[&1].VKORG",
                "@(2,VTWEG)": "[&1].VTWEG",
                "@(2,SPART)": "[&1].SPART",
                "PARVW": "[&1].PARVW",
                "PARZA": "[&1].PARZA",
                "KUNN2": "[&1].KUNN2"
              }
            }
          }
        }
      }
    }
  }
]

Any help is much appreciated


Solution

  • Adding a toList function within in a modify transformation as the first spec will resolve the issue such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "CustomerMaster": {
            "Rootnode": {
              "SalesArea": {
                "PartnerFunction": "=toList"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "CustomerMaster": {
            "Rootnode": {
              "SalesArea": {
                "PartnerFunction": {
                  "*": {
                    "@(3,KUNNR)": "[&1].KUNNR",
                    "@(3,NAME1)": "[&1].NAME1",
                    "@(3,NAME2)": "[&1].NAME2",
                    "@(3,NAME3)": "[&1].NAME3",
                    "@(3,NAME4)": "[&1].NAME4",
                    "@(3,LAND1)": "[&1].LAND1",
                    "@(2,VKORG)": "[&1].VKORG",
                    "@(2,VTWEG)": "[&1].VTWEG",
                    "@(2,SPART)": "[&1].SPART",
                    "PARVW": "[&1].PARVW",
                    "PARZA": "[&1].PARZA",
                    "KUNN2": "[&1].KUNN2"
                  }
                }
              }
            }
          }
        }
      }
    ]