在mule中将数据编辑中的Json转换为xml

时间:2017-04-24 16:50:09

标签: mule dataweave

我正在使用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..

首先,我必须检查哪些受众群体带来的价值,而不是价值来自黑色,而不仅仅是黑人观念,如果价值来自whiteaudience而不是whiteaudience标签将来

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

Cheers,
Bolver

3 个答案:

答案 0 :(得分:0)

检查出来:

%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
            ))
        }
    }
}

答案 1 :(得分:0)

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

   %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 :(得分:0)

检查这是否有帮助:

%dw 1.0

%输出应用程序/ xml

{     &#34; ColuredAudience&#34 ;:     {         (&#34; blackaudience&#34; {

        (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 !="")
    }

    }