Filter and map function in DataWeave 2.0 (Mule 4)

  • February 10, 2021

What is filter?

Filter iterates over an array and applies an expression that returns matching values.

What is map?

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, check out my git repository and Anypoint Exchange.

Username: guestuser1

Password: Muleuser@1

— By Suraj Rohankar