Search code examples
jsonxmldataweavemulesoft

How to Compare json Object value in Xml within Mulesoft


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.


Solution

  • 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]
            }
    }