如何从DataWeave响应中删除空数组对象 [英] How to remove empty array objects from dataweave response
本文介绍了如何从DataWeave响应中删除空数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用例,可以从dataweave响应中删除空的json对象。
I have a use case to remove empty json objects from dataweave response.
转换后的dw响应就像
{
"remuneration": {
"allowance": [
{
}
]
},
"identifiers": {
"employeeId": "1",
"id": "E001",
"payrollId": "901",
"username": "sample"
},
"employment": {
}
}
我希望从输出中删除空对象。
I want the empty objects to be removed from the output.
预期的输出:
{
"identifiers": {
"employeeId": "1",
"id": "E001",
"payrollId": "901",
"username": "sample"
}
}
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)
这篇关于如何从DataWeave响应中删除空数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文