在 Dataweave 中将 JSON 转换为 XML
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="" 。如果该值存在,那么我需要 display.The 动态生成值。
您需要将 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>
遵循@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"))
): {
}
})
}
}
}
}
或者您也可以使用 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($))
})
): {}
})
}
}
}
}
输入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="" 。如果该值存在,那么我需要 display.The 动态生成值。
您需要将 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>
遵循@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"))
): {
}
})
}
}
}
}
或者您也可以使用 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($))
})
): {}
})
}
}
}
}