Dataweave
Whoever amongst all integration developers has started working with MuleSoft regardless of any expertise level, they definitely have heard about DataWeave. DataWeave is the core part of MuleSoft technology which gives you leverage to convert and map your data according to your needs and requirement of the API you’re developing. When we are developing DataWeave codes, are we utilizing the power of our little but strong mapping language?
Skip “null” values to avoid nightmare
Sometimes in course of development, you will find yourself in a position where you will be required to ignore elements or nodes with null or empty values in JSON, XML or whatever format you’re using. You can do it very easily by using one single command called skipNullOn. If you’re wondering how does it work, please bear with me and see the example that I am going to demonstrate.
You have following JSON Input
{
"Employees": [
{
"ID": 100,
"FName": "John",
"LName": "Adelaide",
"DOB": "1987-01-20",
"Address" : [
{
"Street": "20 Eglinton Avenue E",
"Unit": null,
"City": "Toronto",
"Province": "ON",
"Country": "Canada"
},
{
"Street": "20 Eglinton Avenue E",
"Unit": null,
"City": "Toronto",
"Province": "ON",
"Country": "Canada"
}
]
},
{
"ID": 120,
"FName": "Goerge",
"LName": "Smith",
"DOB": "1990-02-15",
"Address" : [
{
"Street": "20 Yonge Street",
"Unit": "1420",
"City": "Toronto",
"Province": "ON",
"Country": "Canada"
}
]
}
]
}
You have to convert it to following output. Just keep in mind that, that first element in Address array in input JSON will be labeled as Home Address and second element in that same array will be labeled as Mailing Address
{
"Employees": [
{
"ID": 100,
"FName": "John",
"LName": "Adelaide",
"DOB": "1987-01-20",
"Address" : [
{
"Street": "20 Eglinton Avenue E",
"Unit": null,
"City": "Toronto",
"Province": "ON",
"Country": "Canada"
},
{
"Street": "20 Eglinton Avenue E",
"Unit": null,
"City": "Toronto",
"Province": "ON",
"Country": "Canada"
}
]
},
{
"ID": 120,
"FName": "Goerge",
"LName": "Smith",
"DOB": "1990-02-15",
"Address" : [
{
"Street": "20 Yonge Street",
"Unit": "1420",
"City": "Toronto",
"Province": "ON",
"Country": "Canada"
}
]
}
]
}
Following simple dataweave code is expected to convert to desired format

After writing the above dataweave code, we get the following result:
{
"EmployeeInformation": [
{
"EmployeeID": 100,
"EmployeeName": {
"GivenName": "John",
"MiddleName": null,
"Surname": "Adelaide"
},
"DateOfBirth": "1987-01-20",
"Addresses": {
"HomeAddress": {
"Street": "20 Eglinton Avenue E",
"Unit": null,
"City": "Toronto",
"Province": "ON",
"Country": "Canada"
},
"MailingAddress": null,
"HomeAddress": null,
"MailingAddress": {
"Street": "20 Eglinton Avenue E",
"Unit": null,
"City": "Toronto",
"Province": "ON",
"Country": "Canada"
}
}
},
{
"EmployeeID": 120,
"EmployeeName": {
"GivenName": "Goerge",
"MiddleName": null,
"Surname": "Smith"
},
"DateOfBirth": "1990-02-15",
"Addresses": {
"HomeAddress": {
"Street": "20 Yonge Street",
"Unit": "1420",
"City": "Toronto",
"Province": "ON",
"Country": "Canada"
},
"MailingAddress": null
}
}
]
}
Please note that the result we’re getting is not correct. Because we can clearly see there are more than one HomeAddress or MailingAddress element that has null value. This is happening inside iteration over address object it checks in each attempt for the index whether it’s 0 or 1 to indicate either it’s an first element or second element. During this checking, either HomeAddress or MailingAddress is set to null. We want to eradicate this problem. One easy way to do this is adding skipNullOn command in this dataweave that we’ve written. Now, let’s see what we can do to resolve the problem we’re having with the JSON output

Only difference is, we’ve added skipNullOn=”everywhere” beside the %output application/json. Now let’s examine the output what we’re getting from this modified dataweave
{
"EmployeeInformation": [
{
"EmployeeID": 100,
"EmployeeName": {
"GivenName": "John",
"Surname": "Adelaide"
},
"DateOfBirth": "1987-01-20",
"Addresses": {
"HomeAddress": {
"Street": "20 Eglinton Avenue E",
"City": "Toronto",
"Province": "ON",
"Country": "Canada"
},
"MailingAddress": {
"Street": "20 Eglinton Avenue E",
"City": "Toronto",
"Province": "ON",
"Country": "Canada"
}
}
},
{
"EmployeeID": 120,
"EmployeeName": {
"GivenName": "Goerge",
"Surname": "Smith"
},
"DateOfBirth": "1990-02-15",
"Addresses": {
"HomeAddress": {
"Street": "20 Yonge Street",
"Unit": "1420",
"City": "Toronto",
"Province": "ON",
"Country": "Canada"
}
}
}
]
}
We’ve achieved the expected output. This is what skipNullOn does, it skips all those elements or attributes with null values in it. This command has three possible values: elements, attributes and everywhere. Value “everywhere” is the combination of both elements and attributes. If you want to achieve the same result only either on elements or attributes, feel free to use respective value for this command. However, other values apart from “everywhere” is more applicable for XML architecture not with JSON format.