1
votes

My Json Payload is

[
    {
        "Customer Contacted": "Rupesh",
        "Inspected": "Mahesh",
        "Lost": "Fire",
        "Job Start Date": "",
        "Work Complete": "",
        "Billing Complete": ""
    }
]

My XML is

%dw 2.0
output application/xml 
---

    {
        root: {
            CustomerContacted: if(payload."Customer Contacted"=="Rupesh") "Hari" 
            else null,
            Inspected: if(payload."Inspected"=="Mahesh")"vamsi"
            else null,
            Lost: payload.Lost,
            JobStartDate: payload."Job Start Date",
            WorkComplete: payload."Work Complete",
            BillingComplete: payload."Billing Complete"
        }
    }

If the values matches from Payload and value provided in If Condition then the value should be changed. After Transformation i am getting below Output.

<?xml version='1.0' encoding='UTF-8'?>
<root>
  <CustomerContacted/>
  <Inspected/>
  <Lost>Fire</Lost>
  <JobStartDate></JobStartDate>
  <WorkComplete></WorkComplete>
  <BillingComplete></BillingComplete>
</root>

I am getting null Values in Customer contacted and Inspected fields but i am expecting the values "Rupesh" and "Mahesh" as "Hari" and "Vamsi"

Kindly let me know what needs to be changed in the XML Code.

2

2 Answers

3
votes

The payload is an array and to match the element to a string, you need to define the index. For example, on your payoad, the Customer Contacted is part of the object and your payload type is an array.

The syntax should be like:

if(payload."Customer Contacted"[0] == "Rupesh") "Hari" 
   else null

Example:

%dw 2.0
output application/xml
---
{
        root: {
            CustomerContacted: if(payload."Customer Contacted"[0] == "Rupesh") "Hari" 
            else null,
            Inspected: if(payload."Inspected"[0]=="Mahesh")"vamsi"
            else null,
            Lost: payload.Lost[0],
            JobStartDate: payload."Job Start Date"[0],
            WorkComplete: payload."Work Complete"[0],
            BillingComplete: payload."Billing Complete"[0]
        }
}
3
votes

Since your input is an array you need to iterate over it to get the values or hardcode the index with array notation [0]. I'd recommend using the map function since hardcoding an index is not extendable in case you get an input array where the size is greater than 1. Something like the following should work for this case:

%dw 2.0
output application/xml 
---
root: {
    subroot: payload map {
        CustomerContacted: if($."Customer Contacted"=="Rupesh") "Hari" else null,
        Inspected: if($."Inspected"=="Mahesh") "vamsi" else null,
        Lost: $.Lost,
        JobStartDate: $."Job Start Date",
        WorkComplete: $."Work Complete",
        BillingComplete: $."Billing Complete"
    }
}

Note: you need the subroot field to output in the case of multiple items in the input array otherwise will get error: Trying to output second root because of invalid xml structure that would result.