Search code examples
dataweavemulesoft

How do I filter the response in dataweave based on a certain criteria?


Lets say I am getting the following response from an API Call.

accounts: [

accountName: "A",
amount: 10$

account Name "B closed",
amount: 20$

]

I want to filter all account responses that contain the closed keyword in the account name. Can anyone tell me how to filter all the responses that contains closed based on this and give me only account A.

I am using dw 1.0 for my mulesoft code. Please let me know if you have any other questions.


Solution

  • Assuming your input is:

    {
      "accounts": [
        {
          "accountName": "A",
          "amount": "10$"
        },
        {
          "accountName": "B closed",
          "amount": "20$"
        }
      ]
    }
    

    Then you can filter it like this:

    %dw 1.0
    %output application/json
    ---
    payload.accounts filter ((account) -> not (account.accountName contains "closed"))
    

    Which will result in:

    [
      {
        "accountName": "A",
        "amount": "10$"
      }
    ]