4

I have done coding in dataweava as

%dw 1.0
%input payload application/xml
%output application/xml skipNullOn="everywhere"
---
{((payload.*Order default []) map {
Order:{
    Channel:$.@EnterpriseCode,
    Code:$.@OrderNo,
    Status:$.@Status,
    OrderLines: {
    (($.OrderLines.*OrderLine default []) map {
    OrderLine:{
        EntryNumber:"abc",
        Status:$.@Status,

        (($.OrderStatuses.*OrderStatus default []) map {

        ShipDate:$.@StatusDate

        }) 
    }})}
}

}
)
}

But its giving error when assigning input as

<?xml version="1.0" encoding="UTF-8"?>
<Order EnterpriseCode="111" OrderNo="222" Status="Scheduled">
    <OrderLines>
        <OrderLine PrimeLineNo="2" Status="Shipped" OrderedQty="1000">

        </OrderLine>
    </OrderLines>
</Order>

Any suggestions here? I have tried default [] but its not working. When assigning null node its giving error. I have tried filter as filter ($ != '')

Rohan Shinde
  • 131
  • 1
  • 4
  • 21
  • 1
    Please provide the example input and output data. – DavoCoder Nov 25 '15 at 19:12
  • What error is it giving? Is it `Cannot coerce a :null to a :array (com.mulesoft.weave.mule.exception.WeaveExecutionException). Message payload is of type: LinkedList` ? – Möoz Dec 01 '15 at 22:32

4 Answers4

2

XML input example:

<?xml version="1.0" encoding="UTF-8"?>
<Order EnterpriseCode="111" OrderNo="222" Status="Scheduled">
  <OrderLines>
    <OrderLine PrimeLineNo="2" Status="Shipped" OrderedQty="1000">
       <OrderStatuses>
          <OrderStatus StatusDate="statusDate"></OrderStatus>
          <OrderStatus StatusDate="statusDate"></OrderStatus>
      </OrderStatuses>
    </OrderLine>
    <OrderLine PrimeLineNo="3" Status="Shipped3" OrderedQty="10003" ></OrderLine>
  </OrderLines>
</Order>

Note: In your example there are spaces between OrderLine open tag and close tag, you have to fix it:

<OrderLine PrimeLineNo="3" Status="Shipped3" OrderedQty="10003" ></OrderLine>

Dataweave script:

%input payload application/xml
%output application/xml skipNullOn="everywhere"
---
{
  ((payload.*Order default []) map {
    Order:{
      Channel:$.@EnterpriseCode,
      Code:$.@OrderNo,
      Status:$.@Status,

      OrderLines: {
        (($.OrderLines.*OrderLine default []) map {

          OrderLine:{
            EntryNumber:"abc",
            Status:$.@Status, 

            (($.OrderStatuses.*OrderStatus default []) map ((key,pos) -> {
                ShipDate:key.@StatusDate
            }) when $!='' otherwise {})

          }

        })
      }


    }
  })
}

You can't map a value if it doesn't exist, so you have to use "when/otherwise" to verify the existence of the elements.

DavoCoder
  • 862
  • 6
  • 17
0

Try this: This should solve your issue. (Unless not/otherwise) or (when/otherwise), any combination can be used as per your requirement. "Unless not" is recommended if ShipDate is present in most cases, else replace "unless not" with "when".

%dw 1.0
%input payload application/xml
%output application/xml skipNullOn="everywhere"
---
{
(
    (payload.*Order default []) map {
        Order: {
            Channel:$.@EnterpriseCode,
            Code:$.@OrderNo,
            Status:$.@Status,
            OrderLines: {
                (
                    ($.OrderLines.*OrderLine default []) map ({
                        OrderLine: {
                            EntryNumber:"abc",
                            Status:$.@Status,
                            (
                                ($.OrderStatuses.*OrderStatus) map {
                                    ShipDate:$.@StatusDate
                                }
                            )
                        }
                    }) unless not $.OrderLines.*OrderLine.OrderStatuses? otherwise {
                        OrderLine: {
                            EntryNumber:"abc",
                            Status:$.@Status                                
                        }
                    }
                )
            }
        }
    }
)
}
J J
  • 63
  • 6
0

Try following approaches:

  • use "SkipNullOn" %output application/xml skipNullOn="everywhere"
  • You can use when condition as shown below yourField: "null" when

payload.yourField == null otherwise payload.yourField

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
0

Giving below the structure definition of Mule Message Object has Message Inbound Property Outbound Property Payload Variable Flow Variable Session Variable Attachment Exception Payload

When a connector of a flow (listening on a port) receives the payload its called Inbound endpoint. When in a flow we have a connectore placed in the middle and send a payload its called Oubound endpoint. Here the all the outbound properties sent to the Http Outbound flow become Inbound Properties within that flow.

For detailed explanation see the link below.

https://docs.mulesoft.com/mule-user-guide/v/3.8/mule-message-structure.

Srinivas
  • 92
  • 3