Reputation: 526
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
Upvotes: 1
Views: 519
Reputation: 2835
Using filter:
%dw 2.0
output application/json
---
payload.productRelation
filter ((item, index) -> item.roles..code contains "test")
map ((item, index) -> item.productNumber)
Upvotes: 4
Reputation:
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
}
)
Upvotes: 2