json - 如何在 mulesoft dataweave 转换中避免 json 有效负载输出中的 null
问题描述
我的输出:
{
"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)
推荐阅读
- c++ - 将 boost 编译为通用库(Intel 和 Apple Silicon 架构)
- javascript - React Native,在 app.js 中导入 routes.js 给出错误
- r - 日期过滤器在 R 中使用或不使用 lubridate 都不起作用
- gradle - 在根项目中找不到任务“运行”
- python - 如何在硒(python)中处理谷歌隐私弹出窗口
- python - 将 JSON 列表转换为 Pandas 数据框
- javascript - 可以在Angular中将多个输入作为数组传递吗?
- opencv - 模板匹配和pyrDown的问题
- kubernetes - Prometheus:在 Prometheus 中找不到来自 pod 的指标
- angular - Angular 在更改时将 ngModel 传递给组件