4 Ways to Check If an Array is Empty in Power Automate

Do you need to test if your array is empty? This post is going to show you how to test for an array with no elements in Power Automate.

When using arrays in Power Automate it can be critical to test if the array is empty.

Your flow might run perfectly when the array contains at least one item, but then fail if the array has no elements.

Checking for an empty array in your flow will allow you to handle errors before they happen. This post will show you how to do that.

Check for an Empty Array with the EMPTY Function

This example shows an array created inside a compose action named myArray. You can create an empty array [], or a non-empty array [1,2,3] using square brackets inside the compose action.

You can then use the empty expression to test if the array is empty or not.

The expression empty(outputs('myArray')) will return true when the myArray is empty and false when the myArray is non-empty.

You can then use this expression in a Condition action and test if it returns true or false.

  1. Set the value to test as the expression empty(outputs('myArray')).
  2. Set the condition type to is equal to.
  3. Set the value to match as true.
  4. Add any action you want to run if the array is empty to the If yes branch of the Condition action.
  5. Add any action you want to run if the array is non-empty to the If no branch of the Condition action.

If myArray is empty then the empty(outputs('myArray')) expression will evaluate to true and the Condition action will run the If yes branch, but if it’s empty the If no actions will run.

This way you can avoid any potential errors by not running actions on an empty array!

Check for an Empty Array with the First Index

Each item in an array can be accessed by referencing its index position in the array. This is how you can get a single value from your arrays.

You can also use this to test if an array is empty. If the first item in the array is null, then the array is empty!

  1. Set the value to test as the expression outputs('myArray')?[0]).

This expression will get the first item in the array by using the zero index [0]. The question mark character ? will ensure the expression doesn’t result in an error if the array is empty and will instead return a null value.

  1. Set the condition type to is equal to.
  2. Set the value to match as null.
  3. Add any action you want to run if the array is empty to the If yes branch of the Condition action.
  4. Add any action you want to run if the array is non-empty to the If no branch of the Condition action.

⚠️ Warning: This test won’t quite work as intended in cases where the first item in the array is null. For example, an array such as [null] or [null,1,2,3] are both not empty but will cause the above test to pass and the If yes actions will run.

Check for an Empty Array with the LENGTH Function

There is another expression you can use to test if an array is empty or not.

The length expression will return the number of characters in a string value, but it will also return the number of elements in an array value. It is a versatile expression.

The expression length(outputs('myArray')) can be used to test if myArray is empty. The expression will return 0 when myArray is empty and a positive whole number when myArray is non-empty.

You can then use this expression in a Condition action and test if it returns true or false.

  1. Set the value to test as the expression length(outputs('myArray')).
  2. Set the condition type is equal to.
  3. Set the value to match to 0.
  4. Add any action you want to run if the array is empty to the If yes branch of the Condition action.
  5. Add any action you want to run if the array is non-empty to the If no branch of the Condition action.

If myArray has no elements, then the length will be 0, and the If yes branch of the condition will run. Otherwise, the length will be greater than 0, and the If no branch will run.

Use the LENGTH Function for a Multiple Condition Test

One benefit of using the length over the empty expression is it will allow you to perform different actions based on the flow having a certain number of elements.

Suppose you want to handle any errors based on an empty array, but then need to perform different actions when the array contains a single element versus when it contains two or more elements.

This can be achieved using a Switch action with the length expression!

  1. Set the On value in the Switch action to the length(outputs('myArray')) expression.
  2. Set the Case value as 0 and add any actions you want to run when the array is empty.
  3. Add a Case 2 option and set the value to 1. Then add any actions you want to run when the array has exactly one element.
  4. Add any actions you want to run when the array has 2 or more elements to the Default switch case.

This setup will allow you to test and handle empty arrays as well as create multiple flow branches based on the count of elements in your array.

Conclusions

Working with arrays in Power Automate is very common, so it’s essential to account for any errors that might happen when you run a flow with an empty array.

Testing whether an array is empty or not is quite easy using either the empty or length expressions.

The length expression has the added benefit of allowing you to test your array for the number of elements it contains, giving you the flexibility to perform multiple conditions.

When you test the array, you can then use a Condition or Switch action to handle your empty array and prevent any run errors.

Do you have any other tips for handling empty arrays in your flows? Let me know in the comments below!

About the Author

John MacDougall

John MacDougall

John is a Microsoft MVP and freelance consultant and trainer specializing in Excel, Power BI, Power Automate, Power Apps and SharePoint. You can find other interesting articles from John on his blog or YouTube channel.

Related Articles

Comments

0 Comments

Get the Latest Tech Tips

Write For Us

Are you a tech enthusiast with a talent for writing great content? Come write for us!

Follow Us

Follow us on social media to stay up to date with the latest in tech!