问题描述
输入JSON是
[{
"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": ""
}
}]
使用下面提到的DW脚本:-
%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"
): {
}
})
}
}
}
}
当转换下面的数据是结果时
<?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>
第二个输入json值为空,然后在输出中我不想显示任何内容,但在输出中显示为 type =“” 。如果存在该值,则需要显示。该值是动态生成的。
解决方法
您需要将if条件包装在密钥对上。
%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" != ""
): {
}
})
}
}
}
}
输出:
<?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>
,
或者,您也可以使用mapObject
函数。
%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($))
})
): {}
})
}
}
}
}
,
对于每个类型属性映射,遵循@Salim Khan在回答中使用的相同逻辑:
%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"))
): {
}
})
}
}
}
}