Power Automate: get value from Array

Why?

In my previous post https://knowhere365.space/power-automate-use-own-array-to-loop-actions/, we used an own Array to loop through data a variable number of times and perform the same step the same variable number of times. Now we want to get data from that loop and use this data outside the loop.

What?

This post will show how we can use data from within the Apply to each loop, outside the loop in a one-time Send an email action.

How?

Remember (or otherwise first read) my previous post https://knowhere365.space/power-automate-use-own-array-to-loop-actions/ where we use the varArrayStrings to loop through:

My first idea was to replace the value of the property ValueFound of each item in the Array with the actual value and then get this property outside the loop. However this requires converting the Array to a String and then converting the String back again to an Array, which can be annoying so I do not recommend this approach 😥.

Therefore I added the property of ArrayIndex for clarification, to show how we can use the integer index of the Array to get the value. Because Power Automate indexes all items in the Array with an own numbering starting at 0, we can reference this integer index to get the value:

1) Add a Compose action in the Apply to each loop:

The expression:

addProperty(items('Apply_to_each_KeyToFind'),'ValueAdded',outputs('Compose_-_FullStringTextResultArray'))

adds a new property ValueAdded to the existing Array varArrayStrings and we combine this new property with the value of the found data we stored in a Compose action.

2) Immediately after the new Compose action above we add an Append to array action to a whole new Array variable: varArrayOutput (do not forget to initialize this new variable before the loop):

By referencing the Compose action the new Array will have a structure like:

[
  {
    "ArrayIndex": 0,
    "KeyToFind": "UserID",
    "ValueFound": "UserIDvalue",
    "ValueAdded": "forward"
  },
  {
    "ArrayIndex": 1,
    "KeyToFind": "Name",
    "ValueFound": "Namevalue",
    "ValueAdded": "Service Account - Sharepoint Migration"
  },
  {
    "ArrayIndex": 2,
    "KeyToFind": "Telephone",
    "ValueFound": "Telephonevalue",
    "ValueAdded": ""
  },
  {
    "ArrayIndex": 3,
    "KeyToFind": "Email",
    "ValueFound": "Emailvalue",
    "ValueAdded": "srv-sharepointmig@tenant.onmicrosoft.com"
  },
  {
    "ArrayIndex": 4,
    "KeyToFind": "Department",
    "ValueFound": "Departmentvalue",
    "ValueAdded": ""
  },
  {
    "ArrayIndex": 5,
    "KeyToFind": "DateOfChange",
    "ValueFound": "DateOfChangevalue",
    "ValueAdded": "2020-04-09"
  },
  {
    "ArrayIndex": 6,
    "KeyToFind": "RequestText",
    "ValueFound": "RequestTextvalue",
    "ValueAdded": "Request about Help FAQ"
  }
]

3) Now we can reference properties in the Array varArrayOutput with an expression like:

variables('varArrayOutput')?[0]?['ValueAdded']

Please note that we do not need the ArrayIndex property to be present within the Array, to reference the values: I just added it here for clarification showing that the numbering starts with 0.

A different approach is to create a separate Object with only the extracted values as properties and reference these object properties directly (big shout out to Thijs Soepenberg for this idea 🤓💪👍):

1) We use an Object variable varObjectOutputValuesOnly, so we initialize it before the loop with just the brackets {}:

2) For this approach we will again use an AddProperty expression:

The expression

addProperty(variables('varObjectOutputValuesOnly'),items('Apply_to_each_KeyToFind')?['KeyToFind'],outputs('Compose_-_FullStringTextResultArray'))

adds a new property in the same Object with the same name as the Key we want to find and also combines this variable property with the value of the found data we stored in already existing Compose action.

3) Immediately after the new Compose action above we add a Set variable action to new Object variable: varObjectOutputValuesOnly:

By referencing the Compose action the Object will have a structure like:

{
  "UserID": "forward",
  "Name": "Service Account - Sharepoint Migration",
  "Telephone": "",
  "Email": "srv-sharepointmig@tenant.onmicrosoft.com",
  "Department": "",
  "DateOfChange": "2020-04-09",
  "RequestText": "Request about Help FAQ"
}

4) Now we can reference the properties in the Object directly using the name of the Key. Just reference the variable and add the name of the Key behind the variable just like we would reference other values:

outputs('Compose_-_varObjectOutputValuesOnly')?['UserID']

So we have two options to reference the data in the loop:

Data from Array 
UserID = @{variables('varArrayOutput')?[0]?['ValueAdded']} 
Email = @{variables('varArrayOutput')?[3]?['ValueAdded']} 

Data from Object 
UserID = @{outputs('Compose_-_varObjectOutputValuesOnly')?['UserID']} 
Email = @{outputs('Compose_-_varObjectOutputValuesOnly')?['Email']}

When copying this code in the Body of an email the result will be:

#WorkSmarter 😎

One thought on “Power Automate: get value from Array

Leave a comment