我需要在dataweave中迭代数据库查询结果并跳过重复项以及基于某些条件的少数元素。比如说,如果列订单的值是这样的: 10-ord,11-ord,12-ord,我应该只选择11-ord并跳过任何其他值以-ord结尾的行。
应该选择其余不以 - ord 结尾的值。
当前代码给出了不同的元素,但不是上面突出显示的条件。目前的代码是:
payload map ((payload, indexOfPayload) -> {
id: payload.orderId ,
number: payload.orderNumber
}) distinctBy $.number
答案 0 :(得分:1)
使用过滤器, distinctBy 和或子句,您可以实现它
%dw 1.0
%var payload = [
{
"id":"value",
"orderNumber": "10-ord"
},
{
"id":"value",
"orderNumber": "11-ord"
},
{
"id":"value",
"orderNumber": "11-ord"
},
{
"id":"value",
"orderNumber": "12-ord"
},
{
"id":"value",
"orderNumber": "13-ord"
},
{
"id":"value",
"orderNumber": "14"
},
{
"id":"value",
"orderNumber": "14"
}
]
%output application/json
---
payload filter (not($.orderNumber contains '-ord')) or $.orderNumber =='11-ord' map ((payload, indexOfPayload) -> {
id: payload.id ,
number: payload.orderNumber
}) distinctBy payload.orderNumber
<强>输出强>
[
{
"id": "value",
"number": "11-ord"
},
{
"id": "value",
"number": "14"
}
]
除了一个
之外,删除了 -ord 的重复值和值答案 1 :(得分:0)
您可以根据“过滤器”子句实现它。我构建了一个示例逻辑,演示了下面的案例:
%dw 1.0
%var payload = [
{
"id":"value",
"orderNumber": "10-ord"
},
{
"id":"value",
"orderNumber": "11"
},
{
"id":"value",
"orderNumber": "12-ord"
},
{
"id":"value",
"orderNumber": "13-ord"
},
{
"id":"value",
"orderNumber": "14-ord"
}
]
%output application/json
---
payload filter (not ($.orderNumber contains '-ord') )
答案 2 :(得分:0)
您应该在映射之前使用过滤器以获得更好的性能。请根据您的要求尝试以下过滤器
payload filter ($.orderNumber contains '-ord') and $.orderNumber == '11-ord' map ((payload, indexOfPayload) -> {
id: payload.orderId ,
number: payload.orderNumber
})
HTH
答案 3 :(得分:0)
%dw 1.0
%var payload = [
{
"id":"value",
"orderNumber": "10-ord"
},
{
"id":"value",
"orderNumber": "11-ord"
},
{
"id":"value",
"orderNumber": "11-ord"
},
{
"id":"value",
"orderNumber": "12-ord"
},
{
"id":"value",
"orderNumber": "13-ord"
},
{
"id":"value",
"orderNumber": "26"
},
{
"id":"value",
"orderNumber": "26"
}
]
%output application/json
---
payload filter (not ($.orderNumber contains '-ord')) or $.orderNumber=='11-ord' map ((payload, indexOfPayload) -> {
id: payload.id ,
number: payload.orderNumber
}) distinctBy payload.orderNumber
答案 4 :(得分:0)
您可以尝试包含或子串登录,如[3..5]匹配ord并过滤JSON对象。