Hello Muleys!
On this submit, we shall be studying about primary and helpful capabilities of DataWeave 2.0 with fast examples.
The record of capabilities used within the article beneath is chosen out from the record of big capabilities out there in DataWeave(Developer’s alternative):
- Be part of (
be a part of
) - Left Be part of (
leftJoin
) - Outer Be part of (
outerJoin
) - Nested be a part of with
map
operator - Replace as Operate
- Replace as Operator
- Max By (
maxBy
) - Min By (
minBy
) - Filtering an array (
filter
) - Map an array (
map
) - DistinctBy an array (
distinctBy
) - GroupBy an array (
groupBy
) - Cut back an array (
cut back
) - Flatten an array (
flatten
)
We could or could not have used the DataWeave perform in our every day integrations. Let’s examine the examples beneath for every perform.
1. Be part of
- The
be a part of
perform behaves equally to a SQL database JOIN. - The
be a part of
perform combines parts of two arrays by matching two ID standards for a similar index in each arrays. - The left and proper arrays should be arrays of objects.
- Importing
core::Arrays
perform is required. - Ignores unmatched objects
DataWeave Code
%dw 2.0
output software/json
import * from dw::core::Arrays
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
---
//be a part of(a, b, (emp) -> emp."Billing Country", (loc)-> loc."Billing Country")
//be a part of(a,b, (a)-> a."Billing Country", (b)-> b."Billing Country")
be a part of(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country")
Output:
[
{
"l": {
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 1000
},
"r": {
"Name": "Shyam",
"Billing City": "HYD",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 3000
}
}
]
2. Left Be part of
- All of the joined objects are returned.
- Importing
core::Arrays
perform is required. - Any unmatched left parts are additionally added.
DataWeave Code
%dw 2.0
output software/json
import * from dw::core::Arrays
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
---
leftJoin(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country")
Output:
[
{
"l": {
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 1000
},
"r": {
"Name": "Shyam",
"Billing City": "HYD",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 3000
}
},
{
"l": {
"Name": "Max",
"Billing City": "NY",
"Billing Country": "USA",
"Message": "Hello world!!",
"Type": "Account",
"Allowance": 2000
}
}
]
3. Outer Be part of
- All of the joined objects are returned.
- Importing the
core::Arrays
perform is required. - Any unmatched left factor or proper parts are additionally added.
DataWeave Code
%dw 2.0
output software/json
import * from dw::core::Arrays
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
---
outerJoin(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country")
Output:
[
{
"l": {
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 1000
},
"r": {
"Name": "Shyam",
"Billing City": "HYD",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 3000
}
},
{
"l": {
"Name": "Max",
"Billing City": "NY",
"Billing Country": "USA",
"Message": "Hello world!!",
"Type": "Account",
"Allowance": 2000
}
}
]
4. Nested Be part of With Map Operator
- Use the
map
perform to iterate over every joined object. - Importing the
core::Arrays
perform is required.
DataWeave Code
%dw 2.0
output software/json
import * from dw::core::Arrays
var a = [{"Name": "Ram","Billing City":"BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City":"HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
---
(be a part of(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country"))
map {
"info": $.l ++ $.r - "Billing Country"
}
Output:
[
{
"info": {
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 1000,
"Name": "Shyam",
"Billing City": "HYD",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 3000
}
}
]
5. Replace as Operate
For Fieldname
- This replace perform updates a discipline in an object with the desired string worth.
- The perform returns a brand new object with the desired discipline and worth.
- Launched in DataWeave model 2.2.2
- Importing the
util::Values
perform is required.
DataWeave Code
%dw 2.0
output software/json
import * from dw::core::Arrays
import * from dw::util::Values
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
enjoyable mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
---
c replace "element" with "abc" //string
Output:
For Index
- Updates an array index with the desired worth
- This replace perform returns a brand new array that adjustments the worth of the desired index.
- Launched in DataWeave model 2.2.2
- Importing the
util::Values
perform is required.
DataWeave Code
%dw 2.0
output software/json
import * from dw::core::Arrays
import * from dw::util::Values
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
enjoyable mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
var d = [1, true, 2, 3, false]
---
d replace 2 with 5 //index
Output:
6. Replace as Operator
- This new replace operator will replace a particular discipline worth with a brand new worth given.
- This characteristic provides a straightforward solution to replace single values in nested information buildings with out requiring to know practical recursion.
- No additional dw libraries are required.
DataWeave Code
%dw 2.0
output software/json
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
enjoyable mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
var d = [1, true, 2, 3, false]
---
c replace {
case factor at .factor -> if (factor == a) "Max" else "Mule"
}
Output:
7. Max By
- Iterates over an array and returns the best worth of comparable parts from it.
- The gadgets should be of the identical kind.
maxBy
throws an error if they don’t seem to be, and the perform returns null if the array is empty.
DataWeave Code
%dw 2.0
output software/json
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
enjoyable mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
---
a maxBy $.Allowance
Output:
{
"Name": "Max",
"Billing City": "NY",
"Billing Country": "USA",
"Message": "Hello world!!",
"Type": "Account",
"Allowance": 2000
}
8. Min By
- Iterates over an array to return the bottom worth of comparable parts from it.
- The gadgets have to be of the identical kind.
minBy
returns an error if they don’t seem to be, and it returns null when the array is empty.
DataWeave Code
%dw 2.0
output software/json
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
enjoyable mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
---
a minBy $.Allowance
Output:
{
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 1000
}
Enter payload (frequent for all capabilities beneath)
[{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},
{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000},
{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000},
{"Name": "John","Billing City": "FL","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 4000}]
9. Filtering an Array (filter)
To filter the info primarily based on the situation.
DataWeave Code
%dw 2.0
output software/json
---
payload filter ((merchandise, index) ->
merchandise."Billing Country" == "India"
)
Output:
[
{
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account"
},
{
"Name": "Shyam",
"Billing City": "HYD",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account"
}
]
10. Map an Array (map)
- Reworking each merchandise in an array
DataWeave Code
%dw 2.0
output software/json
---
payload map ((merchandise, index) ->
{"Cities": if (merchandise."Billing Country" == "USA") "USA" else "Others"}
)
Output:
[
{
"Cities": "Others"
},
{
"Cities": "USA"
},
{
"Cities": "Others"
},
{
"Cities": "USA"
}
]
11. DistinctBy an Array (distinctBy)
Take away duplicate gadgets from an Array.
DataWeave Code
%dw 2.0
output software/json
---
payload distinctBy ((merchandise, index) ->
merchandise."Billing Country"
)
Output:
[
{
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account"
},
{
"Name": "Max",
"Billing City": "NY",
"Billing Country": "USA",
"Message": "Hello world!!",
"Type": "Account"
}
]
12. GroupBy an Array (groupBy)
- Grouping collectively gadgets in an array primarily based on some worth
DataWeave Code
%dw 2.0
output software/json
---
payload groupBy ((merchandise, index) ->
merchandise."Billing Country"
)
Output:
{
"India": [
{
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account"
},
{
"Name": "Shyam",
"Billing City": "HYD",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account"
}
],
"USA": [
{
"Name": "Max",
"Billing City": "NY",
"Billing Country": "USA",
"Message": "Hello world!!",
"Type": "Account"
},
{
"Name": "John",
"Billing City": "FL",
"Billing Country": "USA",
"Message": "Hello world!!",
"Type": "Account"
}
]
}
13. Cut back an Array (cut back)
- It may be used to rework an array to every other kind.
DataWeave Code
%dw 2.0
output software/json
---
payload."Allowance" cut back ((merchandise, accumulator) -> (merchandise + accumulator))
Output:
14. Flatten an Array (flatten)
DataWeave Code
%dw 2.0
output software/json
---
flatten (payload.Title)
Output:
[
"Ram",
"Max",
"Shyam",
"John"
]
Conclusion
As MuleSoft Builders, we use DataWeave codes nearly every day in our integrations. The capabilities talked about above of Array
and examples might assist us obtain our desired outputs/outcomes simply.
Joyful studying!