7

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"
}}
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99
Infinity
  • 484
  • 2
  • 8
  • 21

8 Answers8

11

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.

Ryan Hoegg
  • 2,415
  • 2
  • 14
  • 15
2

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)
)
Jeff Bowman
  • 215
  • 2
  • 11
  • Could you show the transformation to produce the final output (minus the empties)? I think we need to recurse. – Ryan Hoegg Dec 28 '16 at 04:39
  • 1
    I think this would just replace your `acceptable` function, so the rest of the solution should remain as you wrote it. (albeit, might have to invert the truth values) -- updated my answer to show the change to the `acceptable` function. – Jeff Bowman Dec 28 '16 at 04:41
2

Ryan, the function is producing errors in Studio 6.2.3. I had to include an otherwise condition. I had to surround the (key):value in object constructor curly braces, and I had to include an otherwise condition:

%function filterKeyValue(key, value) 
(
  //((key): value) when acceptable(value)
  {(key) : value} when acceptable(value)
  otherwise {}
)
Ethan Port
  • 166
  • 8
  • 1
    A question should not be posted as an answer. You can ask a new question by clicking the [Ask Question](http://stackoverflow.com/questions/ask) button and refer to this post. – Anthony May 05 '17 at 00:00
  • 1
    @Anthony What are you talking about? Ethan's response was reasonable, added perspective to the original answer, and added some helpful commentary. His response was not a direct question. It was a different branch of the conversation. Before you try to police other user's comments maybe you should attempt to parse them first. And maybe you should understand the content instead of judging people. One thing is clear, you are trolling. – granthbr Oct 15 '18 at 07:18
  • @granthbr I apologize for providing a false comment. I did not judge the answerer but misjudged his answer. Unfortunately, there isn't a downvote button on comments, but you could flag my comment and mark it as what you see fit. – Anthony Oct 15 '18 at 11:47
1

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.

abk2192
  • 11
  • 2
1

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
  })
  } 
}
Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52
Zahid
  • 59
  • 10
0

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.

AnupamBhusari
  • 2,395
  • 2
  • 14
  • 22
0

Use this function

%function acceptable(value) (
    !isEmpty(value)
)
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
0

The easiest way for me is keeping the skipNullOn = "everywhere" and then using this if condition:

{ "location": if (payload.address.location.city? or payload.address.location.state?) {
    "city": payload.address.location.city,
    "state": payload.address.location.state}
} else null }
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Sofia
  • 9
  • 2