简体   繁体   中英

Converting JSON into XML in Dataweave

Input JSON is

[{
    "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": ""
    }
}]

Using the DW script mentioned below:-

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

When Transforming the data below is the result

<?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>

The second input json value is empty then in output i dont want to display anything but in my output it is displaying as type="" . If the value is present then i need to display.The value is generated dynamically.

You need to wrap your if condition around the key,vlaue pair.

%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" != ""
                    ): {
                    }
                })
            }
        }
    }
}

Output:

<?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>

Alternatively you could also use the mapObject function.

%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($))
                        })
                    ): {}
                })
            }
        }
    }
}

Follow the same logic that @Salim Khan used in his answer for each type attribute mapping:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM