Azure Logic Apps Toolbox 6-Work with items in Apply to each

2021-03-06 | Toni Pohl

Automation of small tasks is helpful for many recurring processes. Unless you regularly use Power Automate (Flow) or Azure Logic Apps, it is often not that easy to remember the correct syntax for accessing items in a loop. Here is the quick help.

This sample is using a flow in Power Automate to read messages from a Microsoft Teams team (I know, this sounds strange) and outputs any field to an email. The purpose is just to see how to access data in JSON format in a "Apply to each" loop. Here´s the screenshot of the complete flow with the description of the actions.


So, here´s the loop-action, including a variable action.


The "Get messages" shows the data. To access, let´s say, the displayName of the user who wrote the post, we can follow the path as shown below and use the following syntax in the loop:


We access the action with it´s name "Apply to each" and select the "from" key. In that key, there is a "user" object nested. In the "user", there are three more keys nested: id, displayName and userIdentityType. This is how we can navigate through the JSON document.


The "Append to array variable" action only allows to add a value, but fails if the expression is empty. If, for whatever reason, no value is existing, the action throws an error. To work around this, we can use a little trick an add a space and remove that with the trim function later. So, we can use such an expression to avoid any errors.

trim(concat(items('Apply_to_each')['from']?['user']?['displayName'],' '))


So, every user name is added to the list array.


As result, the array is sent in the body of an email, as here:

list items:
["John Doe","","Anne Doe",…]

That´s it. The purpose of this small article was to show the syntax how to work with items in a loop. Hope this small guidance helps for working with flows.

Categories: Azure, Business Intelligence, Cloud, Developer, English, Logic apps, Microsoft, Microsoft365, Office365, Serverless