![](/img/trans.png)
[英]XML to JSON Dataweave Transformatiion issue converting attribute values
[英]Converting JSON into XML in Dataweave
输入 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 条件包裹在 key,vlaue 对周围。
%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"))
): {
}
})
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.