[英]How to remove empty array objects from dataweave response
I have a use case to remove empty json objects from dataweave response. 我有一个用例,可以从dataweave响应中删除空的json对象。
The dw response after transformation will be like 转换后的dw响应将像
{
"remuneration": {
"allowance": [
{
}
]
},
"identifiers": {
"employeeId": "1",
"id": "E001",
"payrollId": "901",
"username": "sample"
},
"employment": {
}
}
I want the empty objects to be removed from the output. 我希望从输出中删除空对象。
Expected output: 预期产量:
{
"identifiers": {
"employeeId": "1",
"id": "E001",
"payrollId": "901",
"username": "sample"
}
}
DWL script DWL脚本
%dw 1.0
%output application/json skipNullOn="everywhere"
---
{
(remuneration: {
(allowance: (payload.remunerations default []) map ((remuneration , indexOfRemuneration) -> {
amount: remuneration.amount,
compensationElement: remuneration.compensationElement,
compensationPlan: remuneration.compensationPlan,
currency: remuneration.currency
}) filter $ != {}) when (sizeOf (payload.remunerations)) != 0
}),
(identifiers: {
employeeId: payload.worker.employeeId,
id: payload.worker.id,
payrollId: payload.worker.payrollId,
username: payload.worker.username
}) when payload.worker != null,
(employment: {
nboxDescription: payload.worker.nboxDescription,
nboxPerformance: payload.worker.nboxPerformance,
nboxPotential: payload.worker.nboxPotential
}) when payload.worker != null
}
There is no out of the box way to do that but I built a function that does that 没有开箱即用的方法可以做到这一点,但是我建立了一个可以做到这一点的函数
%dw 1.0
%output application/json
%function filterEmpty(value)
value match {
object is :object ->
object mapObject {($$) : filterEmpty($)} mapObject ( {($$) : $} when not ($ is :empty) otherwise {}),
array is :array ->
array map filterEmpty($) filter not ($ is :empty)
,
default -> $
}
---
filterEmpty(payload)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.