Try for free Book a demo

Logic App Best Practices, Tips, and Tricks: #29 How to validate if an Array is empty

Microsoft Azure

3 Mins Read

validating empty array 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 the Azure Logic App. Check out these Logic App tips and tricks!

In the last post, we addressed validating whether a string was null or empty. Today I will speak about another good Best practice, Tips, and Tricks that you must consider while designing your business processes (Logic Apps) and another usual validation requirement: How to validate if an Array is empty or not.

validating empty array in logic apps

How to validate if an Array is empty or not.

Another regular validation in our business process is to validate if an Array of objects is null or empty.

This is relatively easy to accomplish in programming languages like C# using a simple condition:

if (myArray == null || myArray.Length == 0)
{ 
     // The array is empty
}

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:

{
    "Process":"List of names",
    "Names": [
        {
            "Name": "Sandro"
        },
        {
            "Name": "Luis"
        }
    ]
}

We want to validate if the List (Array) of Names is null or empty before we can process our message.

Once again, to accomplish that, there are many ways to do so, some more complex than others. Here in this blog post, we will address two of them.

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

  • Array is empty if the Array of Names is null or empty.
  • Array is not empty. It contains X element(s) if the Array of Names contains data.

Approach 1: Using the empty and length functions

In this first approach, we are going to validate using the following compose condition using an OR clause:

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

As you can see in the picture below:

validating null array in logic apps

This is probably the usual way we see when we have the need to do this kind of validation. However, we can do it better and in a more simple and more efficient way.

Approach 2: The best approach using a single statement

In this second and 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:

empty(triggerBody()?['Names']) is equal to true

As you can see in the picture below:

validating empty or null array in logic apps

This condition will do the exact same thing as the previous approach but using a single statement, which will be better in terms of performance and readability.

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 23, 2023.

Azure Logic Apps - Request Demo CTA

Related Articles