Replacing Null with Blank blank in Dataweave 2.0

4.3k views Asked by At

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.enter image description here

2

There are 2 answers

0
Ryan Carter On

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 ""
    )
}
2
Gurpreet Singh On

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