Remove Objects Containing Specified Key-Value Pairs

This DataWeave example removes all objects that contain a set of key-value pairs from an array of objects. Before you begin, note that DataWeave version 2 (%dw 2.0) is for Mule 4 apps. For a Mule 3 app, refer to DataWeave version 1 (%dw 1.0) examples, within the Mule 3.9 documentation set. For other Mule versions, you can use the Mule Runtime version selector in the table of contents.

This example uses the following functions:

  • filter iterates over the objects within the input array to return an array with the objects that return true as the result of an expression. Each object in the input array contains a set of key-value pairs. The expression uses the contains function and the not operator.

  • contains identifies the key-value pairs specified in the dropThese variable. This variable is an array of objects containing the key-value pairs to remove.

    not negates the result of the contains function to make filter remove the key-value pairs identified in dropThese from the final output. (Without not, the result is an array of objects containing only the undesired key-value pairs.)

DataWeave Script:
%dw 2.0
var dropThese = [
{"type" : "secondary", "space" : "rgb"},
{"type" : "primary", "space" : "cmyk"}
]
output application/json
---
payload filter (not (dropThese contains {"type": $."type", "space": $."space"}))
Input XML Payload:
[
    {"sequence": "1", "color": "red", "type": "primary", "space": "rgb"}
    {"sequence": "2", "color": "green", "type": "primary", "space": "rgb"},
    {"sequence": "3", "color": "blue", "type": "primary", "space": "rgb"},
    {"sequence": "4", "color": "yellow", "type": "secondary", "space": "rgb"},
    {"sequence": "5", "color": "magenta", "type": "secondary", "space": "rgb"},
    {"sequence": "6", "color": "cyan", "type": "secondary", "space": "rgb"},
    {"sequence": "7", "color": "cyan", "type": "primary", "space": "cmyk"},
    {"sequence": "8", "color": "magenta", "type": "primary", "space": "cmyk"},
    {"sequence": "9", "color": "yellow", "type": "primary", "space": "cmyk"},
    {"sequence": "10", "color": "red", "type": "secondary", "space": "cmyk"},
    {"sequence": "11", "color": "green", "type": "secondary", "space": "cmyk"},
    {"sequence": "12", "color": "blue", "type": "secondary", "space": "cmyk"}
]
Output XML:
[
  {
    "sequence": "1",
    "color": "red",
    "type": "primary",
    "space": "rgb"
  },
  {
    "sequence": "2",
    "color": "green",
    "type": "primary",
    "space": "rgb"
  },
  {
    "sequence": "3",
    "color": "blue",
    "type": "primary",
    "space": "rgb"
  },
  {
    "sequence": "10",
    "color": "red",
    "type": "secondary",
    "space": "cmyk"
  },
  {
    "sequence": "11",
    "color": "green",
    "type": "secondary",
    "space": "cmyk"
  },
  {
    "sequence": "12",
    "color": "blue",
    "type": "secondary",
    "space": "cmyk"
  }
]

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub