基于嵌套值的Dataweave过滤数组
Dataweave filtering array based on nested values
我正在尝试根据嵌套在对象中的一些值来过滤数组。
我的数据与账户上有未结余额的客户有关。我将天平的年龄嵌套在 10-29 和 31- 的桶中。
我想留住所有账户余额超过 1000 美元的客户。我还想留住 30 天以上账户余额超过 200 美元的客户。这些帐户将被标记。
在示例中,客户 50001 应该从负载中删除,剩下 50002 和 50003:
{
"data": {
"araging": [
{
"customerid": "50001",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "59.2"
},
{
"agingperiod": "31-",
"totalamount": "50.00"
}
]
},
{
"customerid": "50002",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "0.00"
},
{
"agingperiod": "31-",
"totalamount": "246.00"
}
]
},
{
"customerid": "50003",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "1084.60"
},
{
"agingperiod": "31-",
"totalamount": "0.00"
}
]
}
]
}
}
}
我的第一次尝试是在 30 天内让客户的收入超过 200 美元。
%dw 2.0
output application/json
---
{
arAccounts: payload.data.*araging filter ($.aging.totalamount as Number > 200 and $.aging.agingperiod == "31-") map (araging, index) ->
{
customerid: araging.customerid,
totalamount: araging.aging.totalamount as Number
}
}
该代码不断给出有关数组无法与单个值进行比较的消息,而且我不清楚如何遍历“totalamount”的每个值。将类型从 String 更改为 Number 不起作用,因为它是一个数组值。而且我需要知道如何计算这些值的总和,因为任何超过 1000 美元的帐户都会被标记。一个好处是在同一个转换中添加标志字段。
提前致谢。
问题是表达式 $.aging.totalamount
return 是一个数组,而不是数字或字符串。为了检查老化周期总量 and/or 是否符合条件,一种选择是在过滤后检查每个数组是否为空,如以下 DataWeave 表达式中所做的那样:
%dw 2.0
output application/json
---
data: {
araging: (payload.data.araging map
($ ++ {
flagged: !isEmpty($.aging.totalamount filter $ > 1000)
or (
!isEmpty($.aging.agingperiod filter $ == "31-")
and !isEmpty($.aging.totalamount filter $ > 200)
)
})
) filter $.flagged
}
使用提供的输入负载,生成的负载为:
{
"data": {
"araging": [
{
"customerid": "50002",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "0.00"
},
{
"agingperiod": "31-",
"totalamount": "246.00"
}
],
"flagged": true
},
{
"customerid": "50003",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "1084.60"
},
{
"agingperiod": "31-",
"totalamount": "0.00"
}
],
"flagged": true
}
]
}
}
如果您想 return 所有帐户并使用标记的 属性 来了解帐户是否符合条件,您可以从上面的 DataWeave 表达式中删除 filter $.flagged
语句,这将导致:
{
"data": {
"araging": [
{
"customerid": "50001",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "59.2"
},
{
"agingperiod": "31-",
"totalamount": "50.00"
}
],
"flagged": false
},
{
"customerid": "50002",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "0.00"
},
{
"agingperiod": "31-",
"totalamount": "246.00"
}
],
"flagged": true
},
{
"customerid": "50003",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "1084.60"
},
{
"agingperiod": "31-",
"totalamount": "0.00"
}
],
"flagged": true
}
]
}
}
我正在尝试根据嵌套在对象中的一些值来过滤数组。
我的数据与账户上有未结余额的客户有关。我将天平的年龄嵌套在 10-29 和 31- 的桶中。
我想留住所有账户余额超过 1000 美元的客户。我还想留住 30 天以上账户余额超过 200 美元的客户。这些帐户将被标记。
在示例中,客户 50001 应该从负载中删除,剩下 50002 和 50003:
{
"data": {
"araging": [
{
"customerid": "50001",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "59.2"
},
{
"agingperiod": "31-",
"totalamount": "50.00"
}
]
},
{
"customerid": "50002",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "0.00"
},
{
"agingperiod": "31-",
"totalamount": "246.00"
}
]
},
{
"customerid": "50003",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "1084.60"
},
{
"agingperiod": "31-",
"totalamount": "0.00"
}
]
}
]
}
}
}
我的第一次尝试是在 30 天内让客户的收入超过 200 美元。
%dw 2.0
output application/json
---
{
arAccounts: payload.data.*araging filter ($.aging.totalamount as Number > 200 and $.aging.agingperiod == "31-") map (araging, index) ->
{
customerid: araging.customerid,
totalamount: araging.aging.totalamount as Number
}
}
该代码不断给出有关数组无法与单个值进行比较的消息,而且我不清楚如何遍历“totalamount”的每个值。将类型从 String 更改为 Number 不起作用,因为它是一个数组值。而且我需要知道如何计算这些值的总和,因为任何超过 1000 美元的帐户都会被标记。一个好处是在同一个转换中添加标志字段。
提前致谢。
问题是表达式 $.aging.totalamount
return 是一个数组,而不是数字或字符串。为了检查老化周期总量 and/or 是否符合条件,一种选择是在过滤后检查每个数组是否为空,如以下 DataWeave 表达式中所做的那样:
%dw 2.0
output application/json
---
data: {
araging: (payload.data.araging map
($ ++ {
flagged: !isEmpty($.aging.totalamount filter $ > 1000)
or (
!isEmpty($.aging.agingperiod filter $ == "31-")
and !isEmpty($.aging.totalamount filter $ > 200)
)
})
) filter $.flagged
}
使用提供的输入负载,生成的负载为:
{
"data": {
"araging": [
{
"customerid": "50002",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "0.00"
},
{
"agingperiod": "31-",
"totalamount": "246.00"
}
],
"flagged": true
},
{
"customerid": "50003",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "1084.60"
},
{
"agingperiod": "31-",
"totalamount": "0.00"
}
],
"flagged": true
}
]
}
}
如果您想 return 所有帐户并使用标记的 属性 来了解帐户是否符合条件,您可以从上面的 DataWeave 表达式中删除 filter $.flagged
语句,这将导致:
{
"data": {
"araging": [
{
"customerid": "50001",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "59.2"
},
{
"agingperiod": "31-",
"totalamount": "50.00"
}
],
"flagged": false
},
{
"customerid": "50002",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "0.00"
},
{
"agingperiod": "31-",
"totalamount": "246.00"
}
],
"flagged": true
},
{
"customerid": "50003",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "1084.60"
},
{
"agingperiod": "31-",
"totalamount": "0.00"
}
],
"flagged": true
}
]
}
}