DataWeave-这是分组订单的最佳方法,并具有下一个结果m子4
我有以下方案和问题,我尝试通过“ OrderId”进行分组并删除重复字段,但是当我这样做时,我没有正确的结果,因为小组将所有元素都放在同一数组中,而不是元素数组,我将向您展示:
在:
[
{
"orderId":"1110",
"alternateNumber":"#CC16215",
"shippingDate":"2022-04-26",
"sku":"AC27",
"unit":"1",
"salePrice":"19.99"
},
{
"orderId":"1110",
"alternateNumber":"#CC16215",
"shippingDate":"2022-04-26",
"sku":"AC73",
"unit":"1",
"salePrice":"19.99"
},
{
"orderId":"1112",
"alternateNumber":"#CC16215",
"shippingDate":"2022-04-12",
"sku":"OBN03",
"unit":"1",
"salePrice":"19.99"
},
{
"orderId":"1112",
"alternateNumber":"#CC16215",
"shippingDate":"2022-04-26",
"sku":"OB04",
"unit":"1",
"salePrice":"19.99"
}
]
获得:
{
"orderId": "1110",
"alternateNumber": "#CC16215",
"shippingDate": "2022-04-26",
"products": [
{
"sku": "AC27",
"salePrice": "19.99",
"unit": "1"
},
{
"sku": "AC73",
"salePrice": "19.99",
"unit": "1"
}
],
"orderId": "1112",
"alternateNumber": "#CC16215",
"shippingDate": "2022-04-12",
"products": [
{
"sku": "OBN03",
"salePrice": "19.99",
"unit": "1"
},
{
"sku": "OB04",
"salePrice": "19.99",
"unit": "1"
}
]
}
输出预期:
[
{
"orderId":"1110",
"alternateNumber":"#CC16215",
"shippingDate":"2022-04-26",
"products":[
{
"sku":"AC27",
"salePrice":"19.99",
"unit":"1"
},
{
"sku":"AC73",
"salePrice":"19.99",
"unit":"1"
}
]
},
{
"orderId":"1112",
"alternateNumber":"#CC16215",
"shippingDate":"2022-04-12",
"products":[
{
"sku":"OBN03",
"salePrice":"19.99",
"unit":"1"
},
{
"sku":"OB04",
"salePrice":"19.99",
"unit":"1"
}
]
}
]
注意:如果您注意到主差异是元素由单个参数组分开。
我的代码:
payload groupBy ( $.orderId ) mapObject () -> {
"orderId": $[0].orderId,
"alternateNumber": $[0].alternateNumber,
"shippingDate": $[0].shippingDate,
"products": $ map {
"sku": $.sku,
"salePrice": $.salePrice,
"unit": $.unit
}
}
从获得的输出中可以看出,它将所有元素都放在一个元素中,并且不会像预期的输出那样将它们分开,而预期输出的元素则由OrderID分隔,并且它们在数组数组中。任何帮助将不胜感激。谢谢。
I have the following scenario and problem, I try to group by "orderID" and remove the duplicate fields, but when I do it, I don't have the correct result because the group put all the elements in the same array and not as arrays of elements, I will show you:
In:
[
{
"orderId":"1110",
"alternateNumber":"#CC16215",
"shippingDate":"2022-04-26",
"sku":"AC27",
"unit":"1",
"salePrice":"19.99"
},
{
"orderId":"1110",
"alternateNumber":"#CC16215",
"shippingDate":"2022-04-26",
"sku":"AC73",
"unit":"1",
"salePrice":"19.99"
},
{
"orderId":"1112",
"alternateNumber":"#CC16215",
"shippingDate":"2022-04-12",
"sku":"OBN03",
"unit":"1",
"salePrice":"19.99"
},
{
"orderId":"1112",
"alternateNumber":"#CC16215",
"shippingDate":"2022-04-26",
"sku":"OB04",
"unit":"1",
"salePrice":"19.99"
}
]
Output obtained:
{
"orderId": "1110",
"alternateNumber": "#CC16215",
"shippingDate": "2022-04-26",
"products": [
{
"sku": "AC27",
"salePrice": "19.99",
"unit": "1"
},
{
"sku": "AC73",
"salePrice": "19.99",
"unit": "1"
}
],
"orderId": "1112",
"alternateNumber": "#CC16215",
"shippingDate": "2022-04-12",
"products": [
{
"sku": "OBN03",
"salePrice": "19.99",
"unit": "1"
},
{
"sku": "OB04",
"salePrice": "19.99",
"unit": "1"
}
]
}
Output expected:
[
{
"orderId":"1110",
"alternateNumber":"#CC16215",
"shippingDate":"2022-04-26",
"products":[
{
"sku":"AC27",
"salePrice":"19.99",
"unit":"1"
},
{
"sku":"AC73",
"salePrice":"19.99",
"unit":"1"
}
]
},
{
"orderId":"1112",
"alternateNumber":"#CC16215",
"shippingDate":"2022-04-12",
"products":[
{
"sku":"OBN03",
"salePrice":"19.99",
"unit":"1"
},
{
"sku":"OB04",
"salePrice":"19.99",
"unit":"1"
}
]
}
]
note: if you note the principal difference is the elements are separed by individual group of params.
My Code:
payload groupBy ( $.orderId ) mapObject () -> {
"orderId": $[0].orderId,
"alternateNumber": $[0].alternateNumber,
"shippingDate": $[0].shippingDate,
"products": $ map {
"sku": $.sku,
"salePrice": $.salePrice,
"unit": $.unit
}
}
As can be seen in the output obtained, it is putting all the elements as one and it does not separate them as in the expected output, which has elements separated by orderId and they are inside an array of arrays. Any help would be appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试此脚本:
Try with this script: