![](/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.