Reputation: 17
Input JSON is
[{
"Master_Job__r": {
"Customer Contacted__C": "Rupesh",
"Inspected__C": "",
"Lost__C": "Fire",
"Job Start Date__C": "2019-10-25",
"Work Complete__C": "",
"Billing Complete__C": ""
}
}]
Using the DW script mentioned below:-
%dw 2.0
output application/xml
---
{
XACTDOC: {
XACTNET_INFO: {
CONTROL_POINTS: {
(payload map ( payload01 , indexOfPayload01 ) -> {
CONTROL_POINT @("type": if(payload01.Master_Job__r."Customer Contacted__C" != "") payload01.Master_Job__r."Customer Contacted__C" else null,
"type": if (!isEmpty(payload01.Master_Job__r."Inspected__C")) "" else payload01.Master_Job__r."Inspected__C",
"type": payload01.Master_Job__r.Lost__C ,
"type": payload01.Master_Job__r."Job Start Date__C" ,
"type": payload01.Master_Job__r."Work Complete__C" ,
"type": payload01.Master_Job__r."Billing Complete__C"
): {
}
})
}
}
}
}
When Transforming the data below is the result
<?xml version='1.0' encoding='UTF-8'?>
<XACTDOC>
<XACTNET_INFO>
<CONTROL_POINTS>
<CONTROL_POINT type="Rupesh" type="" type="Fire" type="2019-10-25" type="" type=""/>
</CONTROL_POINTS>
</XACTNET_INFO>
</XACTDOC>
The second input json value is empty then in output i dont want to display anything but in my output it is displaying as type="" . If the value is present then i need to display.The value is generated dynamically.
Upvotes: 0
Views: 112
Reputation: 692
Alternatively you could also use the mapObject
function.
%dw 2.0
output application/xml
---
{
XACTDOC: {
XACTNET_INFO: {
CONTROL_POINTS: {
(payload map ( payload01 , indexOfPayload01 ) -> {
CONTROL_POINT @(
(payload01.Master_Job__r mapObject {
("type": $) if(!isEmpty($))
})
): {}
})
}
}
}
}
Upvotes: 2
Reputation: 1296
Follow the same logic that @Salim Khan used in his answer for each type attribute mapping:
%dw 2.0
output application/xml
---
{
XACTDOC: {
XACTNET_INFO: {
CONTROL_POINTS: {
(payload map ( payload01 , indexOfPayload01 ) -> {
CONTROL_POINT @(
("type": payload01.Master_Job__r."Customer Contacted__C") if !isEmpty(payload01.Master_Job__r."Customer Contacted__C"),
("type": payload01.Master_Job__r."Inspected__C") if !isEmpty(payload01.Master_Job__r."Inspected__C"),
("type": payload01.Master_Job__r.Lost__C) if !isEmpty(payload01.Master_Job__r.Lost__C),
("type": payload01.Master_Job__r."Job Start Date__C") if (!isEmpty(payload01.Master_Job__r."Job Start Date__C")) ,
("type": payload01.Master_Job__r."Work Complete__C") if (!isEmpty(payload01.Master_Job__r."Work Complete__C")),
("type": payload01.Master_Job__r."Billing Complete__C") if (!isEmpty(payload01.Master_Job__r."Billing Complete__C"))
): {
}
})
}
}
}
}
Upvotes: 1
Reputation: 4303
You need to wrap your if condition around the key,vlaue pair.
%dw 2.0
output application/xml
---
{
XACTDOC: {
XACTNET_INFO: {
CONTROL_POINTS: {
(payload map ( payload01 , indexOfPayload01 ) -> {
CONTROL_POINT @("type": if(payload01.Master_Job__r."Customer Contacted__C" != "") payload01.Master_Job__r."Customer Contacted__C" else null,
("type": payload01.Master_Job__r."Inspected__C") if payload01.Master_Job__r."Inspected__C" != "" ,
"type": payload01.Master_Job__r.Lost__C ,
"type": payload01.Master_Job__r."Job Start Date__C" ,
("type": payload01.Master_Job__r."Work Complete__C") if payload01.Master_Job__r."Work Complete__C" != "",
("type": payload01.Master_Job__r."Billing Complete__C") if payload01.Master_Job__r."Billing Complete__C" != ""
): {
}
})
}
}
}
}
Output:
<?xml version='1.0' encoding='UTF-8'?>
<XACTDOC>
<XACTNET_INFO>
<CONTROL_POINTS>
<CONTROL_POINT type="Rupesh" type="Fire" type="2019-10-25"/>
</CONTROL_POINTS>
</XACTNET_INFO>
</XACTDOC>
Upvotes: 3