Support Questions

Find answers, ask questions, and share your expertise

JOLT to flatten nested JSON with optional nodes

avatar
New Contributor

Hi All

We are receiving data in JSON format (Nested and optional Nodes). I have written a JOLT to transform/flatten this JSON but not able to get desired result.

I need help to fix JOLT to get desired output.

Input JSON - Node - PartnerFunction and Indicator are optional.

 

{
  "CustomerMaster": {
    "Rootnode": {
      "KUNNR": "0000028790",
      "NAME1": "NV Solucious",
      "LAND1": "BE",
      "SalesArea": [
        {
          "VKORG": "0200",
          "VTWEG": "00",
          "SPART": "00"
        },
        {
          "VKORG": "4932",
          "VTWEG": "00",
          "SPART": "00",
          "AUFSD1": "FT",
          "PartnerFunction": [
            {
              "PARVW": "ZP",
              "PARZA": "000",
              "KUNN2": "0000028790"
            },
            {
              "PARVW": "ZS",
              "PARZA": "000",
              "KUNN2": "90010523"
            }
          ]
        },
        {
          "VKORG": "4932",
          "VTWEG": "10",
          "SPART": "00",
          "Indicator": {
            "IsDistributionCenter": "X"
          },
          "PartnerFunction": {
            "PARVW": "ZS",
            "PARZA": "000",
            "KUNN2": "90010523"
          }
        }
      ]
    }
  }
}

 

 Desired Output - 

 

[ {
  "KUNNR": "0000028790",
  "NAME1": "NV Solucious",
  "LAND1": "BE",,
  "VKORG": "0200",
  "VTWEG": "00",
  "SPART": "00",
}, {
  "KUNNR" : "0000028790",
  "NAME1" : "NV Solucious",
  "LAND1" : "BE",
  "VKORG" : "0200",
  "VTWEG" : "00",
  "SPART" : "00",
  "AUFSD1": "FT",
  "PARVW": "ZP",
  "PARZA": "000",
  "KUNN2": "0000028790"
}, {
  "KUNNR" : "0000028790",
  "NAME1" : "NV Solucious",
  "LAND1" : "BE",
  "VKORG" : "0200",
  "VTWEG" : "00",
  "SPART" : "00",
  "AUFSD1": "FT",
  "PARVW": "ZS",
  "PARZA": "000",
  "KUNN2": "90010523"
}, {
  "KUNNR" : "0000028790",
  "NAME1" : "NV Solucious",
  "LAND1" : "BE",
  "VKORG" : "4932",
  "VTWEG" : "10",
  "SPART" : "00",
  "IsDistributionCenter": "X",
  "PARVW": "ZS",
  "PARZA": "000",
  "KUNN2": "90010523"
}]

 

JOLT Created so far -

 

[
  {
    "operation": "shift",
    "spec": {
      "CustomerMaster": {
        "Rootnode": {
          "*": "else1.&",
          "SalesArea": {
            "*": {
              "*": "&1.else2.&",
              "Indicator": {       //Added the indicator field
                "*": "&2.else2.&"
              },
              "PartnerFunction": {
                "*": {
                  "*": "&3.&2[&1].&"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "PartnerFunction": {
          "*": {
            "@(3,else1)": { "*": "&4[&1].&" },
            "@(2,else2)": { "*": "&4[&1].&" },
            "*": "&3[&1].&"
          }
        }
      }
    }
  },
  { // get rid of the outermost keys
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

 

Using this JOLT , I am getting this output which is not the desired output.

WanderingT_0-1718874505155.png

 

1 REPLY 1

avatar
Community Manager

@WanderingT, Welcome to our community! To help you get the best possible answer, I have tagged in our NiFi experts @SAMSAL @MattWho  who may be able to assist you further.

Please feel free to provide any additional information or details about your query, and we hope that you will find a satisfactory solution to your question.



Regards,

Vidya Sargur,
Community Manager


Was your question answered? Make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.
Learn more about the Cloudera Community: