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

Developer
  • 21
  • 1
  • 7
  • For future reference, don't use images for code, use text. Why not upload images: https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557 – aled Sep 07 '21 at 23:47

2 Answers2

3

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 ""
    )
}
Ryan Carter
  • 11,441
  • 2
  • 20
  • 27
0

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

  • There is when... otherwise in DataWeave 2.0. Your answer mixes DataWeave 1.0 and 2.0 syntax and will not work. – aled Sep 07 '21 at 00:44
  • Also `%output` and `%var` are `output` and `var` in DataWeave 2.0. `:null` should be `Null`. – aled Sep 07 '21 at 23:49