【问题标题】:Transformation of JSON data in mule 4mule 4 中 JSON 数据的转换
【发布时间】:2020-07-22 09:39:37
【问题描述】:

我有一个要求,我必须将 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】:

    你可以使用dynamic object,它基本上会做你想做的事。

    %dw 2.0
    output application/json
    ---
    {
        (payload.Headers.Header map ((item, index) -> {
            (item.Key): item.Value
        })
        )
    }
    

    【讨论】:

      【解决方案2】:

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

      %dw 2.0
      output application/json
      ---
      payload.Header reduce ((item, acc = {}) -> acc ++ {
          (item.Key): item.Value
      })
      

      【讨论】:

        猜你喜欢
        • 2022-09-26
        • 1970-01-01
        • 2020-05-06
        • 1970-01-01
        • 2015-07-16
        • 2020-07-20
        • 2021-02-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多