0

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.

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Salim, Thanks for the update but in input { "Master_Job__r": { "Customer Contacted__C": "", "Inspected__C": "", "Lost__C": "hyh", "Job Start Date__C": "", "Work Complete__C": "", "Billing Complete__C": "" } } ] then received output is <?xml version='1.0' encoding='UTF-8'?> <XACTDOC> <XACTNET_INFO> <CONTROL_POINTS> <CONTROL_POINT type="null" type="hyh"/> </CONTROL_POINTS> </XACTNET_INFO> </XACTDOC> In Input Customer Contacted value is Empty we shouldn't get line but in output i received type= null
2

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.