In this Post we are going to discuss new Features and enhancements introduced in dataweave 2.3.0 in mule 4.3 Version with Examples.
Please subscribe our Youtube channel here for detailed video Tutorials on all topics..
Mule 4.3.0 runtime has introduced very nice enhancements and features of Dataweave. Let’s see them one by one with examples.
nameOf()
This function is used to retrieve the all keys in the form of array of Strings from the input array.It means we can fetch all the indexes of given array.
you can use dwlang.fun tool to play around with dataweave transformations.
Example:
Input:
{
"EmployeeId": 123,
"EmployeeName": "mulesoftTest",
"Address": "Hyderabad",
"Street": "Madhapur",
"POSTALCODE": "500000",
"CITY": "Hyderabad",
"ContactNumber":"9876543210"
}
%dw 2.0
output application/json
---
{
"KeysinPayload" : namesOf(payload)
}
Output:
{
"KeysinPayload": [
"EmployeeId",
"EmployeeName",
"Address",
"Street",
"POSTALCODE",
"CITY",
"ContactNumber"
]
}

valueOf()
This function is used to fetch all values from input payload as string of Array.
Input: Use Same Input above
Dataweave Logic:
%dw 2.0
output application/json
---
{
"ValuesinPayload" : valuesOf(payload)
}
output:
{
"ValuesinPayload": [
123,
"mulesoftTest",
"Hyderabad",
"Madhapur",
"500000",
"Hyderabad",
"9876543210"
]
}

update Operator
This new update operator will update a specific field value with new value given.
Instead of using old map and mapObject syntax by going through key values pairs to update particular value we can use this update operator to achieve this easily.
This feature adds an easy way to update single values in nested data structures without requiring you to understand functional recursion.
Example 1 : update single values
Update specific value in the input payload.Let’s update EmployeeId,Address fields with new values.
If you have saved any value in variable you can refer the new value like vars.newvalue other wise use direct hard coded value as below.
input:
{
"EmployeeId": 123,
"EmployeeName": "mulesoftTest",
"Address": "Hyderabad",
"Street": "Madhapur",
"POSTALCODE": "500000",
"CITY": "Hyderabad",
"ContactNumber":"9876543210"
}
Dataweave code:
%dw 2.0
output application/json
---
payload update {
case Address at .Address -> "New Address"
case EmployeeId at .EmployeeId -> "IN12345Hyd"
}
output:
{
"EmployeeId": "IN12345Hyd",
"EmployeeName": "mulesoftTest",
"Address": "New Address",
"Street": "Madhapur",
"POSTALCODE": "500000",
"CITY": "Hyderabad",
"ContactNumber": "9876543210"
}

Example 2 : Conditional Update
We can use Update Operator to update specific value from the list of input values when some condition is satisfied
input:
[
{
"EmployeeId": 123,
"EmployeeName": "mulesoftTest",
"Address": "Hyderabad",
"Street": "Madhapur",
"POSTALCODE": "500000",
"CITY": "Hyderabad",
"ContactNumber":"9876543210"
},
{
"EmployeeId": 456,
"EmployeeName": "muleTes2",
"Address": "Bangalore",
"Street": "Some Street",
"POSTALCODE": "567865",
"CITY": "Bangalore",
"ContactNumber":"5678456"
}
]
Let’s Update Postal code and city based on below conditions.
Dataweave code:
%dw 2.0
output application/json
---
payload map ((employee)->
employee update {
case POSTALCODE at .POSTALCODE if(POSTALCODE contains "500000")-> POSTALCODE ++ " India"
case CITY at .CITY if(CITY contains "Bangalore")-> CITY ++ " - Karnataka"
})
output:
[
{
"EmployeeId": 123,
"EmployeeName": "mulesoftTest",
"Address": "Hyderabad",
"Street": "Madhapur",
"POSTALCODE": "500000 India",
"CITY": "Hyderabad",
"ContactNumber": "9876543210"
},
{
"EmployeeId": 456,
"EmployeeName": "muleTes2",
"Address": "Bangalore",
"Street": "Some Street",
"POSTALCODE": "567865",
"CITY": "Bangalore - Karnataka",
"ContactNumber": "5678456"
}
]

Example 3: use update operator as upsert.
If the value present in input it will update otherwise it will add new field in the output.
Let’s try to add or update the Email field of our example.(We don’t have email in put so it will create one field while generating output).
Input:
[{
"EmployeeId": 123,
"EmployeeName": "mulesoftTest",
"Address": "Hyderabad",
"Street": "Madhapur",
"POSTALCODE": "500000",
"CITY": "Hyderabad",
"ContactNumber":"9876543210"
},
{
"EmployeeId": 456,
"EmployeeName": "mulesoftTest2",
"Address": "Chennai",
"Street": "Madhapur",
"POSTALCODE": "500000",
"CITY": "Hyderabad",
"ContactNumber":"9876543210",
"EMAILID":"mulesofttestmail@yahoo.com"
}
]
Dataweave Snippet:
%dw 2.0
output application/json
---
payload map ((user) ->
user update {
case EMAILID at .EMAILID! -> if(EMAILID == null) "testmail@gmail.com" else EMAILID
})
output:
[
{
"EmployeeId": 123,
"EmployeeName": "mulesoftTest",
"Address": "Hyderabad",
"Street": "Madhapur",
"POSTALCODE": "500000",
"CITY": "Hyderabad",
"ContactNumber": "9876543210",
"EMAILID": "testmail@gmail.com"
},
{
"EmployeeId": 456,
"EmployeeName": "mulesoftTest2",
"Address": "Chennai",
"Street": "Madhapur",
"POSTALCODE": "500000",
"CITY": "Hyderabad",
"ContactNumber": "9876543210",
"EMAILID": "mulesofttestmail@yahoo.com"
}
]

More features updates coming soon….
