我正在尝试为Dataweave中的数据库结果集执行每个操作,但无法实现结果。
输入有效负载:
{
"x_orders_cursor": [{
"LINE_QTY": 1,
"ORDER_NUMBER": 11820490,
"ORDERED_ITEM": "PAT1M",
"LINE_UOM": "PC"
}, {
"LINE_QTY": 5000,
"ORDER_NUMBER": 11820542,
"ORDERED_ITEM": "ACC62-A-D",
"LINE_NUMBER": 1,
"LINE_UOM": "PC"
}, {
"LINE_QTY": 2000,
"ORDER_NUMBER": 11820542,
"ORDERED_ITEM": "ACC62-A-D",
"LINE_NUMBER": 2,
"LINE_UOM": "PC"
}]
}
预期有效负载:
{
"Orders": [
{
"OrderNum": "11820490",
"Lines": [
{
"LineNum": 1,
"ProductNum": "PAT1M",
"TxnQty": 1,
"TxnQtyUOM": "PC"
}
]
},
{
"OrderNum": "11820542",
"Lines": [
{
"LineNum": 1,
"ProductNum": "ACC62-A-D",
"TxnQty": 1000,
"TxnQtyUOM": "PC"
},
{
"LineNum": 2,
"ProductNum": "ACC62-A-D",
"TxnQty": 2000,
"TxnQtyUOM": "PC"
}
]
}
]
}
结果集基于存储订单号和行号的数据库表。
答案 0 :(得分:0)
您必须使用groupBy
对来自相同订单的行号进行分组,然后对其进行映射。请参考以下代码
%dw 1.0
%output application/json
---
Orders : payload.x_orders_cursor groupBy $.ORDER_NUMBER map {
"OrderNum": $.ORDER_NUMBER[0],
"Lines": $ map (LineDetails,index) ->{
"LineNum": LineDetails.LINE_NUMBER default 1,
"ProductNum": LineDetails.ORDERED_ITEM,
"TxnQty": LineDetails.LINE_QTY,
"TxnQtyUOM": LineDetails.LINE_UOM
} orderBy $.LineNum
} orderBy $.OrderNum
我已使用orderBy
分别订购订单号和行号的订单和订单项
希望这有帮助。
答案 1 :(得分:0)
使用map和groupby运算符执行此操作。应该在下面的行
payload groupBy $.ORDER_NUMBER map {
OrderNum: $.ORDER_NUMBER[0],
Lines": $ map (val,index) -> {
LineNum: val.LINE_NUMBER default 1,
ProductNum: val.ORDERED_ITEM,
TxnQty: val.LINE_QTY,
TxnQtyUOM: val.LINE_UOM
} orderBy $.LineNum
} orderBy $.OrderNum