Power Automate – How to convert an array to a string
Arrays are very convenient when working with (large) sets of data. They are easily accessible and can contain multiple attributes (like working with a table). The downside is that you cannot simply use the array in text-based actions like sending a email.
In this blogpost, I will explain how to convert an array to a string, for both single-attribute arrays and multi-attribute arrays and how to select attributes from a multi-attribute array.
Arrays
First, lets start by explaining the definition of an array in Power Automate.
Like mentioned before, an array can be seen as a list of information. It may contain just a single column dataset, but can has as many columns as you want. These columns are defined as attributes.
An example of a single attribute array:
[ "Power Automate", "SharePoint Online", "Logic Apps", "Power Apps" ]
An example of a multi-attribute array:
[ { "Product": "Power Automate", "Service": "Power Platform" }, { "Product": "SharePoint Online", "Service": "Microsoft 365" }, { "Product": "Logic Apps", "Service": "Azure" }, { "Product": "Power Apps", "Service": "Power Platform" } ]
In which Product and Service are the defined attributes (columns).
There are different ways to use an array:
Self-defined (variables)
An array can be defined yourself by using a variable with type Array. This is the most obvious one. When using this, you determine when, where and how the array should be used. We can use the above examples to build the arrays in Power Automate ourselves:
From (external) data
Another way to use a variable is by calling upon (external) data, like pulling information from a SharePoint list using the Get items action. The result of this action will give you a (multi-attribute) array:
Objects
Something I notice quite often is the confusion between arrays and objects. While both types can store information, an object always contains just a single value, while an array may contain multiple values. Both can have multiple attributes.
Simply said: An object can be seen as a ‘thing’ (value) with ‘characteristics’ (attributes), like a person (value) with blue eyes (attribute) and brown hair (attribute). An array can be seen as a list of ‘things’ using that same characteristics (e.g. a list of people).
Array to string
Like mentioned before, you cannot simply use an array in text-based actions (like sending an email). Or at least, if you want to show them in a neat way:
If you want to use the values stored inside your array in an email in a neat way, you need to convert it to a string first.
Join
The expression used for this is the join() expression. This will pull each value from the array and put it together with a self defined delimiter.
join( <array>, <delimiter> )
The delimiter can be anything you want to make the string look neat. This can be as simple as a comma, but you can also build numbered HTML lists by using <li> and </li> HTML codes.
While this works perfectly for a single-attribute array, the multi-attribute array still doesn’t look the way you want it to be:
This is because the join() expression will only pull each value from the array. Since each value in a multi-attribute array consists of multiple attributes, it will just show that as a string.
HTML table
One way to deal with this is to use the standard Power Automate action called Create HTML table. This will convert the array to an HTML table. By default, it will use all attributes from your array as columns, but you can also define which columns to use by using Advanced options:
When using this, you need to make sure your destination to show the information is completely formatted in HTML as well. For the Send an email action, you need to toggle the Html view, otherwise, it will just show the HTML code.
Select
Sometimes, you don’t want all attributes from a multi-attribute array to show. If you pull data from SharePoint, you will get a lot of system information that might not be interesting.
In my case, I only want the Title column from my SharePoint list (array) to show. To do this, you need to use the Select action.
Within this action, you need to determine the array from which you need to select certain attributes and which attributes you want to select.
- Key states the name of the attribute you want to use. This can be anything you want (making it possible for you to rename an attribute).
- Value states the attribute in which the value is stored in. This needs to be an exact match of your attribute name. When pulling data from an external source (like SharePoint), Dynamic Content will provide you with the possible attributes. Otherwise, you need to manually build the expression: item()?[‘<attribute>’].
When your Select action has been configured, you can use the Create HTML table action again to convert your array to an HTML table. Make sure to select the outcome of your Select action as input for your Create HTML table action.
Multi-attribute array to single attribute string
In the example above, I’ve only selected a single attribute which is shown in a single column HTML table. While this works, you might just want to show the values as a single string. For this, you need to alter the Select action so that you don’t provide a Key and only use the attribute value. You can do this by switching from to Text mode and just provide the attribute (without a name):
This will result in a single-attribute array (without an attribute name):
After this, you can just use the join() expression again (see above) to combine each value into a single string with a delimiter of your choice.