- Subscribe to RSS Feed
- Mark Question as New
- Mark Question as Read
- Float this Question for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
JOLT to flatten nested JSON with optional nodes
- Labels:
-
Apache NiFi
Created 06-20-2024 02:09 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Created 06-20-2024 07:32 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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:
