如何从DataWeave响应中删除空数组对象

时间:2018-10-29 10:05:36

标签: mule dataweave

我有一个用例,可以从dataweave响应中删除空的json对象。

转换后的dw响应就像

{
   "remuneration": {
     "allowance": [
       {   
       }
     ]
   },
   "identifiers": {
     "employeeId": "1",
     "id": "E001",
     "payrollId": "901",
     "username": "sample"
   },
   "employment": {
   }
 }

我希望从输出中删除空对象。

预期输出:

{
   "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
 }

1 个答案:

答案 0 :(得分:0)

没有开箱即用的方式来做到这一点,但我建立了一个可以做到这一点的功能

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