[英]Filter an Array of Json From Mule Dataweave
我的请求 Json 如下所示。 我想检索所有将角色作为测试的 productNumber。
{
"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 过滤器将其过滤掉。 我使用 mule 4 和 dataweave 2.0
使用过滤器:
%dw 2.0
output application/json
---
payload.productRelation
filter ((item, index) -> item.roles..code contains "test")
map ((item, index) -> item.productNumber)
尝试这个:
%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
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.