I am converting Json to XML using Data weave ....need help..
My input json:
if json value is like this
{"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
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 :-
%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
{ "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 !="")
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.