0

我有一个看起来像这样的传入 CSV 文件(注意第一个字段是常见的 - 这是订单号)

36319602,100,12458,HARVEY NORMAN,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
36319602,101,12459,HARVEY NORMAN,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
36319602,102,12457,HARVEY NORMAN, 
36319601,110,12458,HARVEY NORMAN,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
36319601,111,12459,HARVEY NORMAN,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
36319601,112,12457,HARVEY NORMAN, 
36319603,110,12458,HARVEY NORMAN,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
36319603,121,12459,HARVEY NORMAN,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
36319603,132,12457,HARVEY NORMAN, 

这是我当前的 Dataweave 代码

list_of_orders: {
    order: payload map ((payload01 , indexOfPayload01) -> {
        order_dtl: 
        [{
            seq_nbr: payload01[1],
            route_nbr: payload01[2]
        }],
        order_hdr: {
            ord_nbr: payload01[0],
            company: payload01[3],
            city: payload01[4],
        }
    })
}

所需输出的一个例子是这样的......(这只是模拟)。请注意我想要一个按订单号的第一列分组的单个标题 - 但有多个详细信息行


    "list_of_orders": {
      "order": [
        {
          "order_dtl": [
            {
                seq_nbr: 100,
                route_nbr: 12458
            },
            {
                seq_nbr: 101,
                route_nbr: 12459
            },
            {
                seq_nbr: 102,
                route_nbr: 12457
            }                        
          ],
          "order_hdr": 
          {
            ord_nbr: 36319602,
            company: HARVEY NORMAN
          }
        }
      ]
    }

它工作正常,只是它重复 order_hdr 键。他们想要的是一个带有多个详细信息的标题键。分组将基于“ord_nbr:payload01[0]”

任何帮助表示赞赏

谢谢

4

1 回答 1

0

我认为您使用的是 Dataweave 1。在 dw1 中,这groupBy将获得所需的输出(请注意,如果您将字段指针 [0]、1等设置为元数据等,则可以将它们更改为字段名称映射):

%dw 1.0

%output application/json
---
list_of_orders:  {
    order: (payload groupBy ($[0])) map {
    order_dtl: $ map {
        seq_nbr: $[1],
        route_nbr: $[2]
    },
    order_hdr: 
          {
            ord_nbr:  $[0][0],
            company: $[0][3]
          }
 }}

在此处输入图像描述

更新

这是具有多个订单的新输入样本的输出:

{
  "list_of_orders": {
    "order": [
      {
        "order_dtl": [
          {
            "seq_nbr": "110",
            "route_nbr": "12458"
          },
          {
            "seq_nbr": "121",
            "route_nbr": "12459"
          },
          {
            "seq_nbr": "132",
            "route_nbr": "12457"
          }
        ],
        "order_hdr": {
          "ord_nbr": "36319603",
          "company": "HARVEY NORMAN"
        }
      },
      {
        "order_dtl": [
          {
            "seq_nbr": "100",
            "route_nbr": "12458"
          },
          {
            "seq_nbr": "101",
            "route_nbr": "12459"
          },
          {
            "seq_nbr": "102",
            "route_nbr": "12457"
          }
        ],
        "order_hdr": {
          "ord_nbr": "36319602",
          "company": "HARVEY NORMAN"
        }
      },
      {
        "order_dtl": [
          {
            "seq_nbr": "110",
            "route_nbr": "12458"
          },
          {
            "seq_nbr": "111",
            "route_nbr": "12459"
          },
          {
            "seq_nbr": "112",
            "route_nbr": "12457"
          }
        ],
        "order_hdr": {
          "ord_nbr": "36319601",
          "company": "HARVEY NORMAN"
        }
      }
    ]
  }
}
于 2019-05-21T14:01:08.047 回答