How to ignore empty objects in DataWeave Mule esb

I am working on converting my payload. I have a situation here.

The input payload is as follows: -

{ "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 as shown below

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

But I do not want to have an empty location object if all the fields in the location objects are empty. I expect something like this

 { "address": { "city": "bab", "company_name": "asdast", "country_code": "sam" }} 
+5
source share
7 answers

This is a recursive solution that I came to after the direct approach seemed difficult 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 a direct approach that 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 omitted skipNullOn="everywhere" in the %output directive and instead removed the null fields in the function. This allows us to make sure that zeros are removed before we check to see if the containing object is empty.

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

+5
source

This worked for me (NB Dataweave 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 introduce this into Ryan's solution above:

 %function acceptable(value) ( !isEmpty(value) ) 
+2
source

Ryan, this function creates errors in Studio 6.2.3. I had to include another condition. I had to surround (key): the value in curly brackets of the constructor of the objects, and I had to include another condition:

 %function filterKeyValue(key, value) ( //((key): value) when acceptable(value) {(key) : value} when acceptable(value) otherwise {} ) 
+2
source

Unfortunately, none of the solutions worked for me, so I used the second "message conversion" component with the code below and used skipNullOn = "everywhere" in both components. This code recursively searches for empty elements (empty lines, empty lines, empty arrays and empty objects) and deletes them.

 %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 this helps.

+1
source

I have the simplest and 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 }) } } 
+1
source

There is no direct 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.

0
source

Use this function

 %function acceptable(value) ( !isEmpty(value) ) 
0
source

Source: https://habr.com/ru/post/1257861/


All Articles