简体   繁体   中英

Dataweave Error for Null nodes

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 ($ != '')

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.

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                                
                        }
                    }
                )
            }
        }
    }
)
}

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

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 .

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