Krish
Krish

Reputation: 4232

How to apply filter for nested arrays in mulesoft dataWeave

I tried to filter below json payload characters[] array which is having result as 'valid' and data[] array name as 'WBB' and priority as '1'

I tried below code but not working can some one help me ?.

flatten(payload.offers.category.characters) filter ((item, index) -> item.result=='valid' and flatten(item.data) filter ((item, index) -> item.name=='WBB' and item.priority==1))

Json payload

{
  "offers": [
    {
      "id": 100,
      "name": "Test1",
      "category": {
        "characters": [
          {
            "result": "valid",
            "data": [
              {
                "name": "WBB",
                "priority": 1
              },
              {
                "name": "ILL",
                "priority": 2
              }
            ]
          }
        ]
      }
    },
    {
      "id": 200,
      "name": "Test2",
      "category": {
        "characters": [
          {
            "data": [
              {
                "name": "ISS",
                "priority": 1
              },
              {
                "name": "ILL",
                "priority": 2
              }
            ]
          }
        ]
      }
    },
    {
      "id": 300,
      "name": "Test3",
      "category": {
        "characters": [
          {
            "data": [
              {
                "name": "WSS",
                "priority": 1
              },
              {
                "name": "ILL",
                "priority": 2
              }
            ]
          }
        ]
      }
    }
  ]
}

Expected payload

[
 {
  "name": "WBB",
  "priority": 1
 }
]

Upvotes: 0

Views: 495

Answers (2)

subrahmanyam b
subrahmanyam b

Reputation: 21

flatten(payload..data) filter ((item, index) -> item.name == 'WBB' )

Upvotes: 0

Salim Khan
Salim Khan

Reputation: 4303

flatten((flatten(payload.offers..characters) filter $.result == "valid").data) filter ($.name=="WBB" and $.priority  == 1)

Upvotes: 1

Related Questions