在Dataweave中将JSON转换为XML

问题描述

输入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"))
                        ): {
                        }
                    })
                }
            }
        }
    }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...