Author: Suraj Rohankar
What is Filter?
- Iterates over an array and applies an expression that returns matching values.
What is Map?
- Iterates over items in an array and outputs the results into a new array.
Use Case:
A company called ABCD info systems has recorded sales. The company wants to prepare a payload to store the information in a database.
Requirement:
Payload should only contain the Transaction Date, Product Name, Price — where the price is equal to 1200, Country where sales made from United States, Canada, and Spain and payment method should only be Mastercard.
Input Payload:
[
{
“Transaction_date”: “1/2/09 6:17”,
“Product”: “Product1”,
“Price”: “1200”,
“Payment_Type”: “Mastercard”,
“Name”: “John”,
“City”: “London”,
“State”: “England”,
“Country”: “United Kingdom”,
“Account_Created”: “1/2/09 6:00”,
“Last_Login”: “1/2/09 6:08”
},
{
“Transaction_date”: “1/2/09 4:53”,
“Product”: “Product1”,
“Price”: “1200”,
“Payment_Type”: “Mastercard”,
“Name”: “Betina”,
“City”: “Parkville “,
“State”: “MO”,
“Country”: “United Kingdom”,
“Account_Created”: “1/2/09 4:42”,
“Last_Login”: “1/2/09 7:49”
},
{
“Transaction_date”: “1/2/09 13:08”,
“Product”: “Product1”,
“Price”: “1200”,
“Payment_Type”: “Mastercard”,
“Name”: “Federica e Andrea”,
“City”: “Astoria “,
“State”: “OR”,
“Country”: “United States”,
“Account_Created”: “1/1/09 16:21”,
“Last_Login”: “1/3/09 12:32”
},
{
“Transaction_date”: “1/3/09 14:44”,
“Product”: “Product1”,
“Price”: “1200”,
“Payment_Type”: “Visa”,
“Name”: “Gouya”,
“City”: “Echuca”,
“State”: “Victoria”,
“Country”: “Australia”,
“Account_Created”: “9/25/05 21:13”,
“Last_Login”: “1/3/09 14:22”
}
]
Dataweave Script:
%dw 2.0
output application/json
—
payload filter($.Price == “1200” and
($.Country == “United Kingdom” or
$.Country ==”Canada” or
$.Country == “Spain”)
and $.Payment_Type == “Mastercard”) map ((payload, index) ->
{
“Transaction_date”: payload.Transaction_date,
“Product”: payload.Product,
“Price”: payload.Price,
“Country”: payload.Country,
“Payment_Type”: payload.Payment_Type
})
Output Payload:
[
{
“Transaction_date”: “1/2/09 6:17”,
“Product”: “Product1”,
“Price”: “1200”,
“Country”: “United Kingdom”,
“Payment_Type”: “Mastercard”
},
{
“Transaction_date”: “1/2/09 4:53”,
“Product”: “Product1”,
“Price”: “1200”,
“Country”: “United Kingdom”,
“Payment_Type”: “Mastercard”
}
]
Conclusion:
This article explains how we can filter out and map the data by iterating over an array.
- For more examples of MuleSoft, checkout my git repository and anypoint exchange.
Username: guestuser1
Password: Muleuser@1