0

I am new for muleSoft and I have json payload like below ,I want to filter out the below json payload characters[] array which is having name as 'WBB' and priority as '1'

Can some one help me please.

Json Payload


{

 "status": "Success",

 "offers": [

  {

   "id": 100,

   "name": "Test1",

   "category": {

    "characters": [

     {

      "name": "WBB",

      "priority": 1

     },

     {

      "name": "ILL",

      "priority": 2

     }

    ]

   }

  },

  {

   "id": 200,

   "name": "Test2",

   "category": {

    "characters": [

     {

      "name": "WLS",

      "priority": 1

     },

     {

      "name": "DLL",

      "priority": 2

     }

    ]

   }

  },

  {

   "id": 300,

   "name": "Test3",

   "category": {

    "characters": [

     {

      "name": "INTERNET",

      "priority": 1

     },

     {

      "name": "FIBER",

      "priority": 2

     }

    ]

   }
  }
 ]
}

Expected payload


{
 "name": "WBB",
  "priority": 1
}
1
  • is it possible that there are more nesting levels? Commented Sep 13, 2022 at 19:09

1 Answer 1

1

You can simply use filter function include only selected elements from an array. This will give you an array with only elements that satisfy the filter criteria.

Now in your payload the offers is an array and each offer themselves have characters as array, so if you try payload.offers.category.characters you will get an array of characters (i.e. array of array) therefore you will have to flatten it like in the below example.

%dw 2.0
output application/json
---
flatten(payload.offers.category.characters)
filter ((item) -> item.priority == 1 and item.name=="WBB")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.