Try for free Book a demo

Logic App Best Practices, Tips, and Tricks: #28 How to check if a string is Null or Empty

Microsoft Azure

3 Mins Read

How to check if a string is Null or Empty in Logic Apps

Welcome again to another Logic Apps Best Practices, Tips, and Tricks. In my previous blog posts, I talked about some of the essential best practices you should have while working with Azure Logic Apps. Check out these Logic App tips and tricks!

Today I will speak about another useful Best practice, Tips, and Tricks that you must consider while designing your business processes (Logic Apps): How to check if a parameter or a string is Null or Empty.

check empty string in logic app

How to check if a string is Null or Empty

It is normal to validate our business process if a specific string or element/parameter of a message is null or an empty string (“”).

This is relatively easy to accomplish in programming languages like C#, where we can make use, for example, of the String.IsNullOrEmpty(String) method.

if (String.IsNullOrEmpty(s))
 return "is null or empty";
else
 return String.Format("(\"{0}\") is neither null nor empty", s);

This method indicates whether the specified string is null or an empty string (“”),  returning atrue if the value parameter is null or an empty string (“”). Otherwise, false.

But can we achieve the same inside Logic Apps in an easy way?

And the answer is: yes, we can!

Let’s assume that we are going to send this small proof-of-concept JSON payload to our Logic App:

{
    "City":"Porto",
    "Name": "Sandro Pereira"
}

And we want to validate if the Name element is null or empty before we can process our message.

To accomplish that there are many ways to do so, some more complex than others.

For this proof-of-concept, we are going to use a Request-Response Logic App where we will return the following responses:

  • The name field is empty, if the Name element is null or empty, and empty means: an empty string (“”) or a string containing only spaces (”     “).
  • The name <name> is correct, if the Name parameter is valid.

Approach 1: Using the empty, length, trim, and string functions

In this first approach, we are going to validate using the following condition:

empty(triggerBody()?['name']) is equal to true
or length(trim(string(triggerBody()?['name']))) is equal to 0 (zero)

As you can see in the picture below:

checking the string using logic apps

Despite achieving our goal, this condition is a little complicated to validate such a simple expression. However, we can arrange ways to simplify it a little bit.

Approach 2: Using the trim, and string functions

In this second approach, we will simplify our condition by removing the empty function in the first statement and the length function in the second statement. In this case, we are going to validate using the following condition:

triggerBody()?['name'] is equal to null
or trim(string(triggerBody()?['name'])) is equal to ""

As you can see in the picture below:

check empty string in logic app

Still, it is a complex condition to validate a simple expression; and we can do it better!

Approach 3: The best approach using a single statement

In this final approach, we will describe what, to me, is the best approach. That means using only a single statement without any OR clauses.

In this case, we are going to validate using the following condition:

trim(string(triggerBody()?['name'])) is equal to ""

As you can see in the picture below:

check empty string in logic app

This condition does the same as the previous approach, but it is simple and more efficient.

I hope you enjoy this developer tip and stay tuned for the following Logic App Best practices, Tips, and Tricks.

Related reading

This article was published on May 21, 2023.

Related Articles