【问题标题】:Generating all possible combination from two json array list using dataweave使用dataweave从两个json数组列表生成所有可能的组合
【发布时间】:2018-10-18 13:04:32
【问题描述】:

我正在开发 mule 3.9。在工作时,我遇到了一个场景,我想从传入的 JSON 数组列表中生成所有可能的值组合。 json数组列表下的值是动态的。

我想生成 xml 格式的输出。我只想使用 dataweave 来实现这一点。请帮助我如何在 mule 3.9 中使用 datawevae 实现这一目标。

以下是我的 JSON 输入和所需的 xml 输出。

注意:Json 数组列表是动态的

JSON 输入

 "building":[  
      {  
         "code":"BuildingExcess",
         "value":"600"
      },
      {  
         "code":"BuildingExcess",
         "value":"700"
      }
   ],
"content":[  
      {  
         "code":"ContentExcess",
         "value":"600"
      },
      {  
         "code":"ContentExcess",
         "value":"400"
      }
]

输出

  <resultset>
    <list>
        <Building>
            <code>BuildingExcess</code>
            <value>600</value>
        </Building>
        <Content>
            <code>ContentExcess</code>
            <value>600</value>
        </Content>
    </list>
    <list>
        <Building>
            <code>BuildingExcess</code>
            <value>700</value>
        </Building>
        <Content>
            <code>ContentExcess</code>
            <value>600</value>
        </Content>
    </list>
    <list>
        <Building>
            <code>BuildingExcess</code>
            <value>600</value>
        </Building>
        <Content>
            <code>ContentExcess</code>
            <value>400</value>
        </Content>
    </list>
    <list>
        <Building>
            <code>BuildingExcess</code>
            <value>700</value>
        </Building>
        <Content>
            <code>ContentExcess</code>
            <value>400</value>
        </Content>
    </list> 
</resultset>

【问题讨论】:

    标签: mule dataweave mule-esb


    【解决方案1】:

    假设您的两个列表始终是 buildingcontent,并且它们是您的有效负载(或某些变量)的元素,您可以使用以下 dataweave 代码来实现您想要的输出:

    %dw 1.0
    %output application/xml
    %var input = {
        "building":[  
              {  
                 "code":"BuildingExcess",
                 "value":"600"
              },
              {  
                 "code":"BuildingExcess",
                 "value":"700"
              }
           ],
        "content":[  
              {  
                 "code":"ContentExcess",
                 "value":"600"
              },
              {  
                 "code":"ContentExcess",
                 "value":"400"
              }
        ]   
    }
    ---
    resultset: {( 
        flatten (input.content map ((cont) -> input.building map 
            list: {
                Building: $,
                Content: cont
            })
        )
    )}
    

    输出:

    <?xml version='1.0' encoding='windows-1252'?>
    <resultset>
      <list>
        <Building>
          <code>BuildingExcess</code>
          <value>600</value>
        </Building>
        <Content>
          <code>ContentExcess</code>
          <value>600</value>
        </Content>
      </list>
      <list>
        <Building>
          <code>BuildingExcess</code>
          <value>700</value>
        </Building>
        <Content>
          <code>ContentExcess</code>
          <value>600</value>
        </Content>
      </list>
      <list>
        <Building>
          <code>BuildingExcess</code>
          <value>600</value>
        </Building>
        <Content>
          <code>ContentExcess</code>
          <value>400</value>
        </Content>
      </list>
      <list>
        <Building>
          <code>BuildingExcess</code>
          <value>700</value>
        </Building>
        <Content>
          <code>ContentExcess</code>
          <value>400</value>
        </Content>
      </list>
    </resultset>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 2021-03-14
      • 1970-01-01
      相关资源
      最近更新 更多