Mulesoft DataWeave Remove Objects from Array based on Remove Array

Martin Larizzate – Mulesoft Dataweave Remove Objects from Array based on Remove Array

Calculating GIFs | Tenor
Understanding Mulesoft and Dataweave

One way to do that is by using a filter combined with a contain function!

Let’s imagine we have a Persons Array with the fields Name, date-time, and Address. All of these Persons live in Argentina but we want to remove the people who lives in Cordoba Province

Example:

[
  {
    "Name": "Carlos Garcia",
    "date-time": "2024-01-10T20:29:04",
    "address": "False Street 123, Cordoba, Argentina"
  },
  {
    "NAme": "Ernesto Argento",
    "date-time": "2024-01-10T20:31:44",
    "address": "Real Street 123, Buenos Aires"
  }
]

First we need to create a filtered list with all Cordoba’s People

var toRemove = payload filter( $.address contains  'Cordoba')

Our First Result:

[
  {
    "Name": "Carlos Garcia",
    "date-time": "2024-01-10T20:29:04",
    "address": "False Street 123, Cordoba, Argentina"
  }
]

Now that we have filtered the ones we want to delete, the next step is remove them from Payload. so Our code will be:

%dw 2.0
output application/json
var toRemove = payload filter( $.address contains  'Cordoba')
---
payload 
    filter ((item, index) -> not (toRemove.*address contains ( item.address)))

And the Final Result of how to Remove Objects from Array based on Remove Array using Mulesoft and Dataweave:

[
  {
    "NAme": "Ernesto Argento",
    "date-time": "2024-01-10T20:31:44",
    "address": "Real Street 123, Buenos Aires"
  }
]
Playground Example of how to Remove Objects from Array based on Remove Array using Mulesoft and Dataweave

Wrote By:

_- Martin Larizzate -_ Mulesoft Certified Developer, Salesforce Ranger and Salesforce Solution Architect

Deja una respuesta