ule子4中JSON数据的转换

问题描述 投票:0回答:1

我有一个要求,我必须将JSON数据从一种格式转换为另一种格式。我必须获取JSON数组的相应值,并将它们设为键值对。

以下为必需的详细信息:

输入:

"Headers": {
    "Header": [
      {
        "Key": "SellerOrganization",
        "Value": "XYZ"
      },
      {
        "Key": "SellerType",
        "Value": "B2C"
      },
      {
        "Key": "Region",
        "Value": "SOUTH"
      },
      {
        "Key": "OrderType",
        "Value": "RETURN"
      },
      {
        "Key": "InvoiceType",
        "Value": ""
      },
      {
        "Key": "EventType",
        "Value": "Created"
      },
      {
        "Key": "EntryType",
        "Value": "Call Center"
      }
    ]
  }

预期输出:

{
    SellerOrganization:XYZ,
    SellerType: B2C,
    Region:SOUTH,
    OrderType:RETURN,
    InvoiceType:"",
    EventType:Created,
    EntryType:Call Center
}
mule mule-component dataweave mulesoft mule4
1个回答
0
投票

您可以在此处利用reduce功能,将数组转换为key, value对对象

%dw 2.0
output application/json
---
payload.Header reduce ((item, acc = {}) -> acc ++ {
    (item.Key): item.Value
})
© www.soinside.com 2019 - 2024. All rights reserved.