How to Check null condition in Data weaver : Mule

喜你入骨 提交于 2020-01-01 12:09:30

问题


Here is my xml, in that first I need to check 'RecordsEntries' should not be 'null', then RecordEntry shouldn't be null followed by mapping code

<?xml version="1.0" encoding="UTF-8"?>
<Records>
  <storenumber />
  <calculated>false</calculated>
  <subTotal>12</subTotal>
 <RecordsEntries>
   <RecordEntry>
     <deliverycharge>30.0</deliverycharge>
     <entryNumber>8</entryNumber>
     <Value>true</Value>
  </RecordEntry>
  <RecordEntry>
    <deliverycharge>20.0</deliverycharge>
    <entryNumber>7</entryNumber>
    <Value>false</Value>
  </RecordEntry>
</RecordsEntries>
 <RecordsEntries>
    <RecordEntry>
      <deliverycharge>30.0</deliverycharge>
      <entryNumber>8</entryNumber>
      <Value>false</Value>
    </RecordEntry>
 </RecordsEntries>
</Records>

Tried multiple scenario's also used when condition checking but somewhere missing parenthesis or correct format

    orders: {
    order: {
  StoreID: payload.Records.storenumber,
  Total: payload.Records.calculated,
 (( payload.Records.RecordsEntries.*RecordEntry ) map {
    IndividualEntry: {
    Number:$.entryNumber,
    DeliverCharge:$.deliverycharge
    }
  } when ( payload.Records.RecordsEntries != null and payload.Records.RecordsEntries.*RecordEntry !=null))}
}

getting error like missing ). Tried other way around by checking the null condition directly inside the first loop got error like "Cannot coerce array to an boolean". Please suggest. Thanks.


回答1:


You can instead do

%dw 1.0
%output application/xml
---
orders: {
  order: {
  StoreID: payload.Records.storenumber,
  Total: payload.calculated,
 ((payload.Records.*RecordsEntries.*RecordEntry default []) map {
      IndividualEntry: {
        Number:$.entryNumber,
        DeliverCharge:$.deliverycharge
      }
    })
  }
}

DataWeave is "null-safe" for querying values like in payload.Records.*RecordsEntries.*RecordEntry, but an error will be thrown when trying to operate with a null (e.g. null map {}).

The default operator replaces the value to its left, if it's null, with the one on the right.

Also you were missing an *. You need to use it in xml whenever you want all the repetitive elements that match.




回答2:


If you want to skip a particular field assignment in dataweave based on the null or blank , below mentioned script can be used.

payload map ((payload01 , indexOfPayload01) -> { Name: payload01.Name, (LastName: payload01.LastName) when payload01.LastName !=null and payload01.LastName !='' })

This will skip the field mapping for Name , if the name field is coming as null or blank in the incoming payload.



来源:https://stackoverflow.com/questions/32447682/how-to-check-null-condition-in-data-weaver-mule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!