【问题标题】:Output in Json format using dataweave使用 dataweave 以 Json 格式输出
【发布时间】:2016-11-14 06:50:28
【问题描述】:

我正在尝试将字段值映射从 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 的内容不合法(或至少不一致)。您有两个 AccountIDProductIDValue 实例。我猜你的意思是有一个StartDate 的字段和一组结构,每个结构都像一个记录(包含 AccountID、ProductID 和 Value)。
  • 是的@FDavidov - 我希望所有三个字段 AccountId、ProductId 和 value 作为结构数组,按 startdate 分组。
  • 我将添加所需的结构作为答案(这里不可能)。
  • 当然@FDavidov,非常感谢

标签: json dataweave


【解决方案1】:

根据你对我问题的回答,这里是你需要构建的 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

希望这能让你更清楚。

更新: 我错过了闭幕式]。现已添加。

【讨论】:

  • 嗨@FDavidov,我已经粘贴了更新后的代码,但我又得到了相同的结构。请您检查是否遗漏了什么!
  • 恐怕我不知道您用于生成 JSON 的语言,因此我只能猜测(而且很可能会浪费您的时间)。我的直觉告诉我问题出在开头的groupBy。尝试删除/更改它。如果这不起作用......我无法帮助你超越这一点。尽管如此,如果您考虑对我的回答进行投票,我将不胜感激,因为它确实对您有所帮助(识别 JSON 的错误结构)。
  • 没问题@FDavidov,它帮助了我一点,我投票给你。谢谢!
【解决方案2】:

我已经使用了下面的代码,请你确认一下

     %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"
                  ]
                }
              ]
            }   

          ]
        ]
      } 

【讨论】:

  • 现在,您的 JSON 合法 但不是您需要的。您可以使用this 站点来测试生成的 JSON 的结构。如果有错误,它会显示给你。你还有多余的[(例如"AccountID":之后)。
猜你喜欢
  • 1970-01-01
  • 2022-10-05
  • 2021-02-22
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 2019-02-21
  • 2019-01-26
相关资源
最近更新 更多