How to convert array to a string in Power Automate
Posted On January 31, 2021
“I have a SharePoint column with multiple choices enabled, how can I convert the data from it (array) into a string to send it in a single email in Power Automate?”
Power Automate will automatically add ‘Apply to each’ once you try to use dynamic content from an array. It’s fine if you want to process the items one by one, e.g. update multiple items or send multiple emails. But it’s not so good if you need all the items at once. If you’ve got a SharePoint column with multiple choices, you don’t want to loop through them. You want all of the choices at once, a single string in readable format. It can be user information (emails, names), choices, lookups… The usage is the same: don’t loop, convert the values to a single string.
If it’s a simple array with a list of values (no object structure), you can use the join(…) expression. You can see an example of simple array below (created when exporting multiple Person or Group column to .csv):
The join(…) expression expects 2 parameters, the array with values, and a separator for the outcome.
The example below will take the array (variable var_array), and convert into a comma separated string.
join(variables('var_array'), ', ')
variables('var_array') is the array
', ' is definition of the separator, separator are the characters between ' and '
Array with objects
It gets a bit more complicated with more complex arrays: Person or Group column, choice column, etc. Instead of just a list of values, complex arrays contain objects with multiple pairs of “key”: “value”.
Join(…) expression won’t work here as it would create a string from the whole objects. And you don’t want the whole object, you want just a specific value from that array, e.g. Email.
‘Select’ the values
The best approach is to use the ‘Select’ action. The action expects an array of objects on the input, e.g. the array with multiple users, and allows you to select only some values from it. Put the array as the input, in this example it’s the ‘Person_MultipleSelection’ dynamic content. Since you want to build a string only from the values, you should ignore the key/value mapping and switch to the text mode. That way you can skip the key and select only the values.
Each object in the array is represented by the expression item(), and each value by adding ?[‘Key’]. You can use any ‘Key’ from the object. The expressions below will give you all display names or emails of the users.
Output of the ‘Select’ action will be a new array, this time a simple one, that will contain only the desired values.
And as was already explained, a simple array can be converted into a string with the join(…) expression
When you convert an array into a string in Power Automate, the first thing to consider is: “what type of array do I need to process”? If it’s a simple array, you can use the join(…) expression. But if it’s a complex array with objects, it’ll require a ‘Select’ action first.
It’s possible to process complex arrays in a single action with a combination of expressions (as when exporting to .csv), but I believe this step-by-step approach has it’s benefits. You can track what’s happening in each step, it’s easier to debug, and much easier to explain and understand.