簡體   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