在Dataweave 2.0中将Null替换为空白

时间:2019-02-04 05:26:14

标签: mule mule-studio mule-el mule-esb mule-cluster

我必须在dataweave 2.0中用空格替换null,我尝试了许多组合,但都出错了。

随附屏幕截图供您参考。

请提供相同的所有指针。

谢谢。enter image description here

2 个答案:

答案 0 :(得分:1)

这是因为要将空白字符串分配给dValue.doctorId,而不是(doctorId)。同样,在这里使用default更容易设置默认值。这是一个示例:

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

        doctorId : dValue.doctorId default ""
    )
}

答案 1 :(得分:0)

最好使用when-otherwise。 下面是针对您的问题的dataweave转换

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

输出如下:

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

doctorInfoList替换为payload