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