DataWeave-这是分组订单的最佳方法,并具有下一个结果m子4

发布于 2025-01-24 18:52:48 字数 2211 浏览 0 评论 0原文

我有以下方案和问题,我尝试通过“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腹黑女流氓 2025-01-31 18:52:48

尝试此脚本:

payload groupBy ( $.orderId ) pluck $  map (() -> {
  "orderId": $[0].orderId,
  "alternateNumber": $[0].alternateNumber,
  "shippingDate": $[0].shippingDate,
  "products": $ map {
    "sku": $.sku,
    "salePrice": $.salePrice,
    "unit": $.unit
}
})

Try with this script:

payload groupBy ( $.orderId ) pluck $  map (() -> {
  "orderId": $[0].orderId,
  "alternateNumber": $[0].alternateNumber,
  "shippingDate": $[0].shippingDate,
  "products": $ map {
    "sku": $.sku,
    "salePrice": $.salePrice,
    "unit": $.unit
}
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文