Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

Removing Null Values using jolt in a nested JSON response

avatar
Frequent Visitor

Hi, I'm new to jolt and am trying to remove null values from a nested json using jolt. 

 

Example JSON:

{
"field1": {
"subfield1": null
},
"field2": {
"subfield21": "528010000072312",
"subfield22": null
},
"field3": {
"field31": null,
"field32": null,
"field33": {
"field331": null,
"field332": null
}
},
"field4": {
"field41": null,
"field42": "UNKNOWN",
"field43": "NONE",
"field44": {
"field441": {
"field4411": null,
"field4412": null
}
}
},
"field5": {
"field51": null,
"field52": {
"field521": null,
"field522": null
},
"field53": {
"field531": null,
"field532": {
"field5321": null
},
"field533": {
"field5331": "0"
}
}
}

 

 

JOLT SPEC:

 

[
{
"operation": "shift",
"spec": {
"field1": {
"*": {
"*": {
"@1": "&3.&2"
}
}
},
"field2": {
"*": {
"*": {
"@1": "&3.&2"
}
}
},
"field3": {
"*": {
"*": {
"@1": "&3.&2"
},
"field33": {
"*": {
"@1": "&3.&2"
}
}
}
},
"field4": {
"*": {
"*": {
"@1": "&3.&2"
},
"field44": {
"field441": {
"*": {
"@1": "&3.&2"
}
}
}
}
},
"field5": {
"*": {
"*": {
"@1": "&3.&2"
},
"field53": {
"*": {
"*": {
"@1": "&3.&2"
}
}
}
}
},
"*": {
"*": {
"@1": "&2"
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=recursivelySquashNulls"
}
}
]

 

 

OUTPUT:

 

{
"field2" : {
"subfield21" : "528010000072312"
},
"field3" : {
"field33" : [ { }, { } ]
},
"field4" : {
"field42" : "UNKNOWN",
"field43" : "NONE",
"field44" : {
"field441" : { }
}
},
"field5" : {
"field52" : [ { }, { } ],
"field53" : [ {
"field532" : { },
"field533" : {
"field5331" : "0"
}
}, {
"field532" : { },
"field533" : {
"field5331" : "0"
}
}, {
"field532" : { },
"field533" : {
"field5331" : "0"
}
} ]
}
}

 

EXPECTED OUTPUT:

{
"field2": {
"subfield21": "528010000072312"
},
"field4": {
"field42": "UNKNOWN",
"field43": "NONE"
},
"field5": {
"field53": {
"field533": {
"field5331": "0"
}
}
}

 

QUESTIONS:

1. How can I completely remove an empty node in json using jolt?

2. Why is the values for "field5" being repeated?

Would very much appreciate help with Jolt 🙂 

1 REPLY 1

avatar
Super Guru

@AmiJhones ,

 

I've answer a similar question before: https://community.cloudera.com/t5/Support-Questions/Json-Jolt-to-Remove-All-Nulls-from-Json/td-p/336...

 

Cheers,

André

 

--
Was your question answered? Please take some time to click on "Accept as Solution" below this post.
If you find a reply useful, say thanks by clicking on the thumbs up button.