繁体   English   中英

使用dataweave以Json格式输出

[英]Output in Json format using dataweave

我正在尝试将字段值映射从CSV转换为Json格式,下面的代码是我的dataweave代码,用于将字段从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"
                  ]
                }
              ]
            }   

          ]
        ]
      } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM