How to check “if null” in Dataweave

Posted by

How to check “if null” in Dataweave

To check if something is null, variables, payload, attributes, whatever you’re checking for, might not be a straightforward path in Dataweave: there are several ways to do so, and some of them could be confusing.

In this article, we’ll discover them together, but before we begin, let me introduce myself.

My name is Lorenzo Neri, and I’m a Cap4 Lab integration engineer. Several times during my working time, I met many different problems I tried to solve, then afterward, a question came to my mind: “What if I bring help to other people to solve my same problem?”.

Let’s get started.

How to check if something is null in Dataweave: comparison

The first option offered by Dataweave is the comparison operator

Nutshell said you could directly check if your variable, payload fields, attributes, or the payload entirely, is null or not, thanks to the equality.

To get deeper, let me guide you with the following example payload:

{
    "field1": "String",
    "field2": "",
    "field3": [],
    "field4": null
}

If you’re willing to check if the field named “field1” exists or not, which means if it’s null or it’s not null, you need a simple thing in your Dataweave expression:

%dw 2.0
output application/json
---
payload.field1 == null

Which returns “false”.

The opposite check you could do is using the “!=” operator.

Now, let’s have a look at the following two fields: “field2” and “field3”. Let’s properly organize the outcome:

%dw 2.0
output application/json
---
{
    "outcome_field2": payload.field2 == null,
    "outcome_field3": payload.field3 == null
}

Both outcomes will be “false”!

Here comes a substantial difference to be discussed.

How to NOT check if something is null in Dataweave: isEmpty function

Dataweave offers a function named “isEmpty”. As its name suggests, this function checks if its parameters are empty or not, which does NOT mean it’s null!

Let me show you the following example taken from the previously shown payload:

%dw 2.0
output application/json
---
{
    "outcome_field2": isEmpty(payload.field2),
    "outcome_field3": isEmpty(payload.field3)
}

Both fields will be valued as “true” because they’re valued but empty.

To explain this difference even better, here it comes a simple but excellent example:

A simple but effective example offered by this Reddit user 

A simple but effective example offered by this Reddit user

 

Respectively, “field2” and “field3” are empty string and empty array: that’s why they’re not null, but the “isEmpty” function returns “true”.

At the same time, why shouldn’t you use the “isEmpty” function to check if something is null in Dataweave?

Because it’s a fake friend: if you consider the payload we have been using since the beginning of the article and try to do like so:

 

%dw 2.0
output application/json
---
isEmpty(payload.not_existsing_field)

It returns “true” while it shouldn’t.

The field “not_existing_field” is not even defined in the payload! 

It should throw an error because it considers something “empty” while it is literally “null”.

Now that we understand this point, let’s see another way to check if something is null in Dataweave.

Question mark operator in Dataweave to check whether it is null or not

Dataweave offers an additional way to check this: the question mark operator.

It’s a shrank way of checking if something is null or exists.

For instance, if we try to do like so, considering always the original payload:

%dw 2.0
output application/json
---
{
    "outcome_field1": payload.field2?,
    "outcome_field2": payload.field45?
}

“outcome_field1” is valued as “true” while “outcome_field2” is valued as “false”.

In a nutshell, the question mark operator in Dataweave returns “trueif the considered value exists. Differently returns “false”.