从 Mule Dataweave 中过滤 Json 的数组
Filter an Array of Json From Mule Dataweave
我的请求Json 如下所示。我想检索所有具有测试角色的产品编号。
{
"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"
}
}
]
}
]
}
我尝试使用dataweave filter 将其过滤掉。我使用 mule 4 和 dataweave 2.0
试试这个:
%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
}
)
使用过滤器:
%dw 2.0
output application/json
---
payload.productRelation
filter ((item, index) -> item.roles..code contains "test")
map ((item, index) -> item.productNumber)
我的请求Json 如下所示。我想检索所有具有测试角色的产品编号。
{
"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"
}
}
]
}
]
}
我尝试使用dataweave filter 将其过滤掉。我使用 mule 4 和 dataweave 2.0
试试这个:
%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
}
)
使用过滤器:
%dw 2.0
output application/json
---
payload.productRelation
filter ((item, index) -> item.roles..code contains "test")
map ((item, index) -> item.productNumber)