【问题标题】:Convert Json to xml in dataweave in mule在 mule 中的 dataweave 中将 Json 转换为 xml
【发布时间】:2017-04-24 16:50:09
【问题描述】:

我正在使用 Data weave 将 Json 转换为 XML ....需要帮助..

我的输入json:

如果json值是这样的

 {"whiteaudience": [
   {
    "audienceType": {
      "Id": "70000",
      "Name": "whiteau"

    }
  },
  {
    "audienceType": {
      "Id": "70000",
      "Name": "whiteau"

    }
  }
],
"blackaudience": [
  {
    "audienceType": {
      "Id": "",
      "Name": ""

    }
  },
  {
    "audienceType": {
      "Id": "",
      "Name": ""

    }
  }
]
}

thnan output XML be like 

<ColuredAudience>
    <whiteaudience>
        <item>70000</item>
        <item>70000</item>
    </whiteaudience>
</ColuredAudience>


and if input json is like this 

{"whiteaudience": [
  {
    "audienceType": {
      "Id": "",
      "Name": ""

    }
  },
  {
    "audienceType": {
      "Id": "",
      "Name": ""

    }
  }
],
"blackaudience": [
  {
    "audienceType": {
      "Id": "80000",
      "Name": " blackau"

    }
  },
  {
    "audienceType": {
      "Id": "80000",
      "Name": "blackau"

    }
  }
]
}
thnan output XML be like 

<ColuredAudience>
    <blackaudience>
        <item>80000</item>
        <item>80000</item>
    </blackaudience>
</ColuredAudience>


So the logic is ,i will get value(s) either for whiteaudience.id or  blackaudience.id  so if the values is coming for whiteaudience than blackauidence tag will come with empty tag(s) and viceversa..

所以首先我必须检查哪些受众标签具有价值,而不是价值来自 blackaudience,而不是只有 blackaudience 来,如果价值来自 whiteaudience,那么 whiteaudience 标签会来

Please advice how to do mapping and use filter in such senarios

Cheers,
Bolver

【问题讨论】:

    标签: mule dataweave


    【解决方案1】:

    根据您的要求,您可以在 Dataweave 中使用 if 条件跳过值为"" 的元素并轻松获得预期结果强> :-

       %dw 1.0
       %output application/xml 
       ---
       {
            ColuredAudience: {
              include: { 
                  (payload.whiteaudience map 
                  {(item: $.audienceType.Id) when $.audienceType.Id !=""}),
    
                  (payload.blackaudience map 
                  {(item: $.audienceType.Id) when $.audienceType.Id != ""})
                }
              }
          } 
    

    【讨论】:

      【解决方案2】:

      检查这是否有帮助:

      %dw 1.0
      

      %输出应用程序/xml

      { “有色观众”: { (“黑观众”:{

              (payload.blackaudience  map (
                  "item": $.audienceType.Id
              )
      
              )
              }) when (payload.blackaudience[0].audienceType.Id !="")
              ,
      
      
          ("whiteaudience":{
              (payload.whiteaudience  map (
                  "item": $.audienceType.Id
              )
              )
              } )  when (payload.whiteaudience[0].audienceType.Id !="")
          }
      
          }
      

      【讨论】:

        【解决方案3】:

        看看这个:

        %dw 1.0 
        %output application/xml
        ---
        {
            "ColuredAudience": {
                "include":{
                    (payload.whiteaudience filter ($.audienceType.Id !="") map (
                        "item": $.audienceType.Id
                    )),
                    (payload.blackaudience filter ($.audienceType.Id !="") map (
                        "item": $.audienceType.Id
                    ))
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-02-04
          • 1970-01-01
          • 2022-01-09
          • 1970-01-01
          • 2021-02-22
          • 2020-04-13
          • 2017-05-20
          • 1970-01-01
          相关资源
          最近更新 更多