1
votes

My Request Json Looks like below. I want to retrieve all the productNumber which has got the roles as test.

{
   "productRelation": [
      {
         "productNumber": 12345,
         "roles": [
            {
               "role": {
                  "code": "test",
                  "description": "test"
               }
            }
         ]
      },
      {
         "productNumber": 789,
         "roles": [
            {
               "role": {
                  "code": "dev",
                  "description": "dev"
               }
            }
         ]
      },
      {
         "productNumber": 111,
         "roles": [
            {
               "role": {
                  "code": "prod",
                  "description": "prod"
               }
            }
         ]
      }
   ]
}

I tried using dataweave filter to filter it out. I use mule 4 and dataweave 2.0

2

2 Answers

4
votes

Using filter:

%dw 2.0
output application/json
---
payload.productRelation 
    filter ((item, index) -> item.roles..code contains "test") 
    map ((item, index) -> item.productNumber)
2
votes

Try this:

%dw 2.0
output application/json
---
payload.productRelation reduce (e, acc=[]) -> (
    do {
        // Get the roles for the productNumber]
        var roles = e.roles..*code
        ---
        // if the roles contain test add the productNumber to the result
        if (roles contains "test") (acc + e.productNumber) else acc
    }
)