繁体   English   中英

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

[英]how to avoid null in json payload output in mulesoft dataweave transform

我的输出:

   {
            "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否则为“”),但是我必须写在每个领域之后。 我们有更好的解决方案吗? 任何线索将不胜感激

这将为您工作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM