简体   繁体   English

在m子中的dataweave中将Json转换为xml

[英]Convert Json to xml in dataweave in mule

I am converting Json to XML using Data weave ....need help.. 我正在使用数据编织将Json转换为XML ..需要帮助..

My input json: 我的输入json:

if json value is like this 如果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..

so firstly i have to check which audiencetags is coming with value than if value is coming from blackauidence than only blackaudience will come and if value is coming from whiteaudience than whiteaudience tag will come 所以首先,我必须检查哪些受众群体标签具有价值,而不是如果价值来自blackauidence,那么只有黑受众会来,如果价值来自whiteaudience,那么whiteaudience标签会来

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

Cheers,
Bolver

Check this out: 看一下这个:

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

As per your requirement, you can use if condition in Dataweave to skip the elements having value as "" and get your expected result easily :- 根据您的要求,您可以使用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 != ""})
            }
          }
      } 

Check if this helps: 检查是否有帮助:

%dw 1.0

%output application/xml %输出应用程序/ XML

{ "ColuredAudience": { ("blackaudience":{ {“” ColuredAudience“:{(”“ blackaudience”:{

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM