使用 dataweave 以 Json 格式输出

Output in Json format using dataweave

我正在尝试将字段值映射从 CSV 格式转换为 Json 格式,下面的代码是我的数据编织代码,用于从 CSV 映射字段并将其转换为 Json 格式:

%dw 1.0    
%output application/json    
---    
{

"volume":
[       
    payload groupBy $.StartDate map ((val,cal) ->
    {
        StartDate:val.StartDate[0],

        rows :
        [
            {
                AccountID : val.AccountID,
                ProductID : val.ProductID,
                Value : val.Value
            }
        ]
    }
    )
]    
}

我得到如下输出:-

{

"volume": [
[
  {
    "StartDate": "8/1/2016",
    "AccountID": [
      "16482965",
      "16482966"
    ],
    "ProductID": [
      "12235398476-AR02",
      "12235398477-AR03"
    ],
    "Value": [
      "1720",
      "1722"
    ]
  },
  .
  .
  .

但我希望我的输出如下所示:

  {

"volume": [
[
  {
    "StartDate": "8/1/2016",
    "AccountID":"16482965","ProductID":"12235398476-AR02","Value":"1720",
    "AccountID":"16482966","ProductID":"12235398477-AR03","Value": "1722"
   },
     .
     .
     .

这里有人可以吗?

根据你对我问题的回答,这里是你需要构建的JSON结构:

{
    "volume": [
                {
                    "StartDate": "8/1/2016",
                    "Entries": [
                                {"AccountID":"16482965","ProductID":"12235398476-AR02","Value":"1720"},
                                {"AccountID":"16482966","ProductID":"12235398477-AR03","Value":"1722"}
                               ]
                },
                ...
              ]
     .
     .
     .
}

注意结构中添加了 "Entries" 元素。它将允许您 运行 通过使用像这样的引用来遍历数组:

...volume[n].Entries[j].AccountID

希望这能让你更清楚。

更新: 我错过了结束语 ]。现在添加。

我用过下面的代码,请确认一下好吗

     %dw 1.0
     %output application/json
     ---
     {
        "Transaction":"111",
        "type":"b002",
        "volume":
        [       
            payload groupBy $.StartDate map ((val,cal) ->
            {
                StartDate:val.StartDate[0],

                "Entries" :
                [
                    {
                        AccountID : val.AccountID,
                        ProductID : val.ProductID,
                        Value : val.Value
                    }
                ]
            }
            )
        ]
     }

And iam still getting the out put as :

            {
        "Transaction": "111",
        "type": "b002",
        "volume": [
          [
            {
              "StartDate": "8/1/2016",
              "Entries": [
                {
                  "AccountID": [
                    "16482965",
                    "16482966"
                  ],
                  "ProductID": [
                    "12235398476-AR02",
                    "12235398477-AR03"
                  ],
                  "Value": [
                    "1720",
                    "1722"
                  ]
                }
              ]
            },
            {
              "StartDate": "7/31/2016",
              "Entries": [
                {
                  "AccountID": [
                    "16482964"
                  ],
                  "ProductID": [
                    "12235398475-AR01"
                  ],
                  "Value": [
                    "1720"
                  ]
                }
              ]
            }   

          ]
        ]
      }