A
anonymous
Guest
anonymous Asks: Comparing two json objects and delete null values from the second json object
I want to compare 2 objects and merge them with patch json data. Comparing payload and patch and remove fields from payload if the values are null in patch object.
}
but I am unable to remove null value from location-1 object in the output. TIA
I want to compare 2 objects and merge them with patch json data. Comparing payload and patch and remove fields from payload if the values are null in patch object.
Code:
Input:
{
"firstname":"John",
"lastname":"Sam",
"city": "San Jose",
"path": "additional",
"location-1":
{
"address": "USA",
"pin": null
},
"location-2":
{
"address": null,
"place": "abc",
"pin": null
}
}
Code:
Dataweave Code:
%dw 2.0
output application/json
import mergeWith from dw::core::Objects
var patch = {
lastname:"Courtney",
age:"18",
path: "",
company: "MR",
firstname: null,
city: null,
"location-1": {
"address": "USA",
"pin": null
}
}
---
(payload mergeWith ((patch)) filterObject ($ != null ))
Expected Output:
{
"location-2": {
"address": null,
"place": "abc",
"pin": null
},
"lastname": "Courtney",
"age": "18",
"path": "",
"company": "MR",
"location-1": {
"address": "USA"
}
}
but I am unable to remove null value from location-1 object in the output. TIA
SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.