Hello friends,

In this article I will show you how we can filter results from complex Arrays. In this use-case we will use select and descendent operators from Mulesoft dataweave.

In this use-case we will try to find out the Book details which is at-least rented once from Library. We will see some variations to dataweave expressions in below section.

Input Payload:

[
  {
    "bookId": "20200114",
    "bookType": "Fiction",
    "title": {
      "en": "Candide"
    },
    "message": {
      "en": ""
    },
    "bookDetails": [
      {
        "label": {
          "en": "Candide"
        },
        "Library": {
          "city": "Pune",
          "rented": {
            "count": "1"
          }
        }
      }
    ]
  },
  {
    "bookId": "20200115",
    "bookType": "Fiction",
    "title": {
      "en": "The Alchemist"
    },
    "message": {
      "en": ""
    },
    "bookDetails": [
      {
        "label": {
          "en": "The Alchemist"
        },
        "Library": {
          "city": "Kolkata",
          "rented": {
            "count": "0"
          }
        }
      }
    ]
  }
]
DW Script:
%dw 2.0
output application/json
---
payload[?($.bookDetails.Library.rented.count[0] >= "1")] default []
Output:
[
  {
    "bookId": "20200114",
    "bookType": "Fiction",
    "title": {
      "en": "Candide"
    },
    "message": {
      "en": ""
    },
    "bookDetails": [
      {
        "label": {
          "en": "Candide"
        },
        "Library": {
          "city": "Pune",
          "rented": {
            "count": "1"
          }
        }
      }
    ]
  }
]

Now let us understand the dataweave script in some details:

filter-complex-array-dw-script
DW-Script

We can add multiple conditions inside red brackets based on our requirement.

For example if we want to add filter books based on Library location. We can use
expression like below:
%dw 2.0
output application/json
---
payload[?(($.bookDetails.Library.rented.count[0] >= "1") and ($.bookDetails.Library.city[0] == "Pune"))] default []
%dw 2.0
output application/json
---
payload[?(($.bookDetails.Library.rented.count[0] >= "1") and ($.bookDetails.Library.city contains "Pune"))] default []
Here, important thing to understand about [0] inside red-brackets. Actually we either need
to use [0], because as we have already said in diagram that it iterates and evaluates each
record and give output result or contains operator to evaluate the condition. Also, when
we are having any count check and compare like “> or <” contains operator will not work.
When we are using selectors for filtering data with one more variation in expression.
Let’s see some more variation where we can use descendent operator to evaluate
the results.
%dw 2.0
output application/json
---
payload[?(($..count[0] >= "1") and ($..city[0] == "Pune"))] default []
%dw 2.0
output application/json
---
payload[?(($..count[0] >= "1") and ($..city contains "Pune"))] default []
Results will remain same with expression used with descendent and contains operator.
Happy Learning:)

By Manish Kumar

I am having around 10 years of IT experience in Integration Architecture, Requirement gathering, Effort Estimation, Application Design\Development\Testing and Deployment including 5+ years of experience in MuleSoft ESB and Hybrid Integrations. DevOps and Cloud Integration is my area of interest.

One thought on “How To Filter Complex Array Using Dw2-Mule4”

Leave a Reply

Your email address will not be published. Required fields are marked *