简体   繁体   中英

Replacing Null with Blank blank in Dataweave 2.0

I have to replace null with blanks in dataweave 2.0 , I tried many combinations but getting error in it .

The screenshot is attached for your reference .

Please provide any pointers for the same .

Thanks. 在此处输入图片说明

Its because you are assigning the blank string to dValue.doctorId and not (doctorId). Also using default is easier here for setting default values. Here is an example:

%dw 2.0
output application/xml
var doctorInformationList=[{doctorId: '1'},{doctorId: '2'}, {}]
---
root: {
        DoctorInformationList: doctorInformationList map ((dValue, dIndex) -> 

        doctorId : dValue.doctorId default ""
    )
}

It's better to use when - otherwise . Below is dataweave transform for your problem

%dw 2.0
%output application/json
%var doctorInfoList=[{doctorId: '1', doctorName : 'A'},{doctorId: '2', doctorName : 'B'}, 
    {doctorId: null, doctorName : 'C'},{doctorId: '', doctorName : 'D'},{}]
---
{
        DoctorInfoList: doctorInfoList map ((doctorValue, doctorIndex) -> {
            "docorId" : '' when doctorValue.doctorId is :null otherwise doctorValue.doctorId,
            "docorName" : doctorValue.doctorName 
        }
     )
}

Output would be like :

{
  "DoctorInfoList": [
    {
      "docorId": "1",
      "docorName": "A"
    },
    {
      "docorId": "2",
      "docorName": "B"
    },
    {
      "docorId": "",
      "docorName": "C"
    },
    {
      "docorId": "",
      "docorName": "D"
    },
    {
      "docorId": "",
      "docorName": null
    }
  ]
}

Replace doctorInfoList with your payload

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