Skip to main content
SAMSAL
Platinum ⭐️⭐️⭐️
Feb 10, 2022 6:16 PM UTC
Solved

Json Jolt to Remove All Nulls from Json

Hi,

 

I have the following Json. I would like to create a Jolt Spec to remove all nulls from all levels. How can I do that? Any help would be appreciated. Thanks

{
"ID": "c22b657e-227b-4c65-e6f8-08d88a3e1118",
"Name": "SomeName",
"TaggedItemName": "SomeValue",
"ITRName": "SomeValue",
"TestReference": null,
"JobCardName": null,
"Comments": "test",
"PrimaryHandoverName": null,
"SecondaryHandoverNumber": null,
"CertificationGroupingName": null,
"DocumentCode": "SomeValue",
"AssignedToName": "SomeName",
"DocumentReference": null,
"TagITRCompletionStatusName": "Rejected",
"ScheduleRevision": null,
"Completed": {
"SignOff_AuthP": "John",
"SignOff_Date": null
},
"Accepted": {
"SignOff_AuthP": "Smith",
"SignOff_Date": null
},
"Approved": {
"SignOff_AuthP": "Ali",
"SignOff_Date": null
},
"ScheduleGroup": null,
"DownloadUri": "SomeUrl",
"UpdatedDate": "2022-01-18T11:36:33.9057626Z",
"CreatedDate": "2020-11-30T10:57:20.8534287Z"
}

araujo
araujoAnswer
Platinum ⭐️⭐️⭐️
Feb 11, 2022 9:51 PM UTC

Great to hear!

 

I try my best to understand Jolt because sometimes it can be quite useful, but I think it has a very convoluted syntax and sometimes it's really hard to use. But practice helps.

 

The first asterisk matches against the field names of an object. The second asterisk depends: if the value of the attribute is a scalar, it will match against the value; if it's a nest object, it will match against the name of the nested object. The trick is that when it matches the value of the object it does not match nulls 😉

 

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.

3 replies

araujo
Platinum ⭐️⭐️⭐️
Platinum ⭐️⭐️⭐️
Feb 11, 2022 2:45 AM UTC

I don't know if it's possible to do this generically and recursively using Jolt.

Nevertheless, if your schema is well know you can achieve want you want with something like this:

{
"Completed": {
"*": {
"*": {
"@1": "&3.&2"
}
}
},
"Accepted": {
"*": {
"*": {
"@1": "&3.&2"
}
}
},
"Approved": {
"*": {
"*": {
"@1": "&3.&2"
}
}
},
"*": {
"*": {
"@1": "&2"
}
}
}

 

Cheers,

Andre

Translate
-- 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.
SAMSAL
SAMSALAuthor
Platinum ⭐️⭐️⭐️
Feb 11, 2022 7:10 PM UTC

Thank you so much,

 

That did the trick. You seem to be a json jolt guru. Im trying to understand it myself but its not that straight forward. Can you just elaborate this syntax for me which remove null values on the base level:

"*": {
"*": {
"@1": "&2"
}
}

Why there is two asterisks in front of it?

Translate
araujo
Platinum ⭐️⭐️⭐️
araujoAnswer
Platinum ⭐️⭐️⭐️
Feb 11, 2022 9:51 PM UTC

Great to hear!

 

I try my best to understand Jolt because sometimes it can be quite useful, but I think it has a very convoluted syntax and sometimes it's really hard to use. But practice helps.

 

The first asterisk matches against the field names of an object. The second asterisk depends: if the value of the attribute is a scalar, it will match against the value; if it's a nest object, it will match against the name of the nested object. The trick is that when it matches the value of the object it does not match nulls 😉

 

Cheers,

André

Translate
-- 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.