2

Im kinda new to MuleSoft and DataWeave and im trying to make a JSON Object with only not null values from another JSON object.

Let's say this is my JSON array :

{
    str1 : "String 1",
    str2 : "String 2",
    str3 : null,
    str4 : "String 4",
}

I want to make a copy of that JSON array but without str3, so result should looks like this :

{
    str1 : "String 1",
    str2 : "String 2",
    str4 : "String 4",
}

Anyone can help me out with this ? or atleast lead me to the solutions ?

Regards

Fragan
  • 792
  • 10
  • 29

3 Answers3

4

There are 2 possible approaches:

Using the writer property skipNullOn as mentioned here

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

Programatically with an if condition (this is for field by field mapping)

var b = null
---
{
    a: 1,
    (b: b) if b != null,
    c: 3
}
Jorge Garcia
  • 1,313
  • 8
  • 14
3

For an object you can transform all the attributes that are not null:

%dw 2.0
output application/json
---
payload mapObject (($$): $ ) if (!($ == null))
aled
  • 21,330
  • 3
  • 27
  • 34
3

Here's yet another way:

%dw 2.0
output application/json
var o = {
    str1 : "String 1",
    str2 : "String 2",
    str3 : null,
    str4 : "String 4",
}
---
o filterObject $ != null

Here's the filterObject documentation

Pick the one you like :)

George
  • 2,758
  • 12
  • 16