cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

JSOn body for property with null value

nluu
New Contributor II

Hi all,
Iโ€™m having an issue while designing the mapping property with null value for my JSON object. For example I have a Mapper to map $input to $body with the expression like follow:
{โ€œmy_idโ€: $input.Id,
โ€œmy_nameโ€: $input.Name,
โ€œmy_addressโ€: $input.Address
}.
When the $input is {โ€œIdโ€: 123,โ€œNameโ€: null, โ€œAddressโ€: โ€œA Street, B District, C Cityโ€}, the $body should be: {โ€œmy_idโ€: 123,
โ€œmy_nameโ€: null,
โ€œmy_addressโ€: โ€œA Street, B District, C Cityโ€
}. But when the $input is {โ€œIdโ€: 123, โ€œAddressโ€: โ€œA Street, B District, C Cityโ€}, the $body should be:
{โ€œmy_idโ€: $input.Id,
โ€œmy_addressโ€: $input.Address
}. I tried with hasOwnProperty() and hasPath(), but they all treats the null value as not existing property, so it will not work.

Does anyone know how can I keep the null value for mapping, but eliminate property that does not exist in the $input. Thank you in advanced.
best regards.

3 REPLIES 3

bojanvelevski
Valued Contributor

Hi @nluu,

You can try the following expression:

$input.mapKeys((v,k)=> "my_"+k.toLowerCase())

I believe it should do the work.
Regards,

Bojan

nluu
New Contributor II

Hi @bojanvelevski,

thank you for your response. It actually a very good solution. But in my case, the name is not always in the format of โ€œmy_โ€ + key. The name will depends on the naming I agreed with the Database team and the CRM team, so sometime it will be like โ€œclose_dateโ€: $input.ProductEffectiveDate_c etc. But Iโ€™ll try to base on your suggestion to find out the solution. Thank you so much
Regards

alchemiz
Contributor III

Hope this helps ๐Ÿ˜€

{โ€œmy_idโ€: โ€œIdโ€, โ€œmy_nameโ€: โ€œNameโ€, โ€œmy_addressโ€: โ€œAddressโ€ }.mapValues((val,key)=> $input.get(val,{})).filter((val,key)=> (!(val instanceof Object)||!(val.isEmpty())))

image