How to ignore empty objects in DataWeave Mule esb

2019-02-10 01:18发布

I am working on transforming my payload. I have got the situation here.

Input payload looks like this below one:-

{
 "address": {
    "city": "bab",
    "company_name": "asdast",
    "country_code": "sam",
    "location": {
    "city": null,
    "state": null
  }
}}

I used %output application/json skipNullOn = "everywhere" it returns me JSON like below

{
 "address": {
"city": "bab",
"company_name": "asdast",
"country_code": "sam",
"location": { }
}}

But I don't want to have empty location object if all fields in location objects are empty.I am expecting something like this

{   
"address": {
"city": "bab",
"company_name": "asdast",
"country_code": "sam"
}}

7条回答
smile是对你的礼貌
2楼-- · 2019-02-10 01:25

There is no straight way to do this, You can do something like this

%dw 1.0
%output application/json
--- 
address:  payload.address - "location" when (sizeOf (payload.address.location pluck $ filter $ != null)) == 0 otherwise payload

Hope this helps.

查看更多
Summer. ? 凉城
3楼-- · 2019-02-10 01:29

This is a recursive solution I arrived at after the direct approach seemed hard to understand:

%dw 1.0
%output application/json

%function acceptable(value) (
    (value default {}) != {}
)

%function filterKeyValue(key, value) (
    ((key): value) when acceptable(value)
)

%function removeFields(o) o
    unless o is :object
    otherwise o mapObject
        (filterKeyValue($$, removeFields($)))

---
removeFields(payload)

Here's the direct approach I started with:

%dw 1.0
%output application/json

%function skipNulls(o) o 
    unless o is :object 
    otherwise o mapObject {
        (($$): $) when ($ != null)
    }

%function skipEmpty(o) o mapObject {
        (($$): $) when ($ != {})
    }

---
address: skipEmpty(payload.address
    mapObject { 
        ($$): skipNulls($)
    }
)

Note that we dropped skipNullOn="everywhere" on the %output directive, and removed null fields in a function instead. This allows us to make sure the nulls get removed before we check if the containing object is empty.

The function (in both solutions) works because mapObject allows us to loop over the object fields, and include them in the result object only if they meet a certain condition.

查看更多
The star\"
4楼-- · 2019-02-10 01:34

This worked for me (N.B. Dataweave is from Mule version 3.8):

%function isEmpty(thing) thing match {
  :null -> true,
  arr is :array -> arr == [],
  obj is :object -> obj == {},
  '' -> true,
  /\s+/ -> true,
  default -> false
}

UPDATE:

So, to inject this in the solution by Ryan above:

%function acceptable(value) (
    !isEmpty(value)
)
查看更多
三岁会撩人
5楼-- · 2019-02-10 01:44

I have the simplest and the easiest solution.

%dw 1.0
%output application/json skipNullOn = "everywhere"
---
{
    "address": {
    "city": payload.address.city,
    "company_name": payload.address.company_name,
    "country_code": payload.address.country_code,
    ("location": {
        "city": payload.address.location.city,
        "state": payload.address.location.state
  })
  } 
}
查看更多
仙女界的扛把子
6楼-- · 2019-02-10 01:46

Use this function

%function acceptable(value) (
    !isEmpty(value)
)
查看更多
一纸荒年 Trace。
7楼-- · 2019-02-10 01:48

Unfortunately, none of the solutions worked for me , so I used a second 'transform message' component with below code and used skipNullOn="everywhere" in both the components. This code recursively searches for empty elements(null fields,empty strings,empty arrays and empty objects) and removes it.

%dw 1.0
%function removeEmptyInArray(arr) arr map (
    (removeEmptyInArray($) when $ is :array 
    otherwise (removeEmptyInObject($) when $ is :object
    otherwise $ when ($ != null and (sizeOf $) > 0) otherwise null))
) when arr != [] 
otherwise null
%function removeEmptyInObject(obj) obj mapObject (
    '$$': (removeEmptyInArray($) when $ is :array 
    otherwise (removeEmptyInObject($) when $ is :object
    otherwise $ when ($ != null and (sizeOf $) > 0) otherwise null))
)

%output application/json skipNullOn="everywhere"
---

removeEmptyInObject(payload)

Hope it helps.

查看更多
登录 后发表回答