【问题标题】:Create json array using dataweave使用 dataweave 创建 json 数组
【发布时间】:2018-03-27 15:58:02
【问题描述】:

如果我有这样的 xml....

<Root>
  <Authority>Water</Authority>
  <Sanctions>
    <Sanction>
      <SanctionCode>11</SanctionCode>
      <SanctionDesc>First Sanction</SanctionDesc>
    </Sanction>
    <Sanction>
      <SanctionCode>11</SanctionCode>
      <SanctionDesc>Second Sanction</SanctionDesc>
    </Sanction>          
  </Sanctions>
</Root>

使用 DataWeave 如何仅使用 SanctionDesc 创建一个 Santion 的 json 数组?

这个我试过了,但是不对...

%dw 1.0
%output application/json
---
records: payload.Root map {
   Authority: $.Authority,
   sanctions: $.Sanctions.Sanction map [$.SanctionDesc]
}

我希望我的输出看起来像这样......

{
    "records": [{
        "Authority": "Water",
        "sanctions": ["First Sanction", "Second Sanction"]
    }]
}

【问题讨论】:

    标签: mule dataweave


    【解决方案1】:

    试试这个

    %dw 1.0
    %output application/json
    ---
    records: {
       Authority: payload.Root.Authority,
       sanctions: payload.Root.Sanctions..SanctionDesc
    }
    

    或者

    %dw 1.0
    %output application/json
    ---
    records: {
       Authority: payload.Root.Authority,
       sanctions: payload.Root.Sanctions.*Sanction map $.SanctionDesc
    }
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢。请问Sanction前面的*是什么意思?仅仅是每个人的意思吗?我一直试图在某个地方找到一个定义,但一直找不到一个好的定义。
    • * 用于 dataweave 的选择器之一。更多详情请参考Multi Value selector
    【解决方案2】:

    在从输入负载中挑选和选择元素时,了解地图运算符是关键。映射运算符遍历左侧的所有数组元素,我们可以从右侧选择值,以 Mulesoft 门户为例

    %dw 1.0

    %输出应用程序/json

    users: ["john", "peter", "matt"] 地图 ((firstName, position) -> position ++ ":" ++ upper firstName)

    输出: { “用户”:[ "0:约翰", “1:彼得”, “2:亚光” ] }

    见以下链接: https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#map

    【讨论】:

      猜你喜欢
      • 2021-01-07
      • 2019-08-05
      • 1970-01-01
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      相关资源
      最近更新 更多