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
}
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")