如何在Mulesoft DataWeave转换中避免JSON有效负载输出中的null

时间:2018-08-25 12:50:37

标签: json dataweave

我的输出:

   {
            "EmployeePositions": {
              "EmployeeID": "05383803",
              **"EmployeeName": null,**
              "Positions": [
                {
                  "PositionID": null,
                  "EffectiveDate": "2017-06-17",
                  "BusinessUnit": null,
                  "Customer": {
                    **"JobReqNumber": null,**
                    "CustomerID": "243720",
                    "CustomerName": "TEKGS UK@BANK OF AMERICA",
                    "CustomerOrderNumber": null
                  },
                  "WorkSiteLocaiton": {
                    "Address1": "BLUE SQUARES HOUSE",
                    "Address2": "PRIORS WAY",
                    "Address3": " ",
                    "Address4": " ",
                    "Number1": " ",
                    "Number2": " ",
                    "City": "BERKS",
                    "State": "BRACKNELL",
                    "Postal": "RG42 ING",
                    "Country": "GBR"
                  }
                },
                {
                  "PositionID": null,
                  "EffectiveDate": "2017-06-15",
                  "EffectiveStatus": "I",
                  "BusinessUnit": null,
                  "Customer": {
                    "JobReqNumber": null,
                    "CustomerID": "243721",
                    "CustomerName": "TEKGS UK@UK Bank",
                    "CustomerOrderNumber": null
                  },
                  "WorkSiteLocaiton": {
                    "Address1": "BLUE SQUARES",
                    "Address2": "PRIORS WAY",
                    "Address3": " ",
                    "Address4": " ",
                    "Number1": " ",
                    "Number2": " ",
                    "City": "BERKSS",
                    "State": "BRACKNEL",
                    "Postal": "RG42 ING",
                    "Country": "GBR"
                  }
                }
              ]
            }
          }

预期输出:

   {
            "EmployeePositions": {
              "EmployeeID": "05383803",
              "EmployeeName": "John Smith",
              "Positions": [
                {
                  "PositionID": "",
                  "EffectiveDate": "2017-06-17",
                  "BusinessUnit": "",
                  "Customer": {
                    "JobReqNumber": "",
                    "CustomerID": "243720",
                    "CustomerName": "TEKGS UK@BANK OF AMERICA",
                    "CustomerOrderNumber": ""
                  },
                  "WorkSiteLocaiton": {
                    "Address1": "BLUE SQUARES HOUSE",
                    "Address2": "PRIORS WAY",
                    "Address3": " ",
                    "Address4": " ",
                    "Number1": " ",
                    "Number2": " ",
                    "City": "BERKS",
                    "State": "BRACKNELL",
                    "Postal": "RG42 ING",
                    "Country": "GBR"
                  }
                },
                {
                  "PositionID": "",
                  "EffectiveDate": "2017-06-15",
                  "EffectiveStatus": "I",
                  "BusinessUnit": "",
                  "Customer": {
                    "JobReqNumber": "",
                    "CustomerID": "243721",
                    "CustomerName": "TEKGS UK@UK Bank",
                    "CustomerOrderNumber": ""
                  },
                  "WorkSiteLocaiton": {
                    "Address1": "BLUE SQUARES",
                    "Address2": "PRIORS WAY",
                    "Address3": " ",
                    "Address4": " ",
                    "Number1": " ",
                    "Number2": " ",
                    "City": "BERKSS",
                    "State": "BRACKNEL",
                    "Postal": "RG42 ING",
                    "Country": "GBR"
                  }
                }
              ]
            }
          }

我想要完全相同的输出,而不是null我想要(“”)作为输出 我尝试了(%output application / json skipNullOn =“ everywhere”)并且也(当有效载荷!= null否则为“”),但是我必须在每个字段之后写上它。我们有更好的解决方案吗? 任何线索将不胜感激

1 个答案:

答案 0 :(得分:0)

这将为您工作:

%dw 1.0
%output application/json

%function applyToValues(e, fn)
  e match {
    :object  -> $ mapObject {($$): applyToValues($, fn)},
    :array   -> $ map applyToValues($, fn),
    default  -> fn($)
  }

%function replaceNull(e, replacement="")
  applyToValues(e, ((v) -> replacement when (v == null) otherwise v))
---
replaceNull(payload)
相关问题