Get the first item or first file from an array in Power Automate

October 13th 2020
Back to all blogs

We provide three separate split actions for Power Automate, which allow you to split a PDF document using myriad options such as barcodes, search text, regular expression matches, page number, page array and bookmarks… phew!

All of these actions will always return an array of documents. Even if the collection only contains one document, it is still an array, and accessing the record without an ‘Apply to each’ loop is a common question this post should hopefully answer.

Get the first item in an array.

To explain how Power Automate treats an array of data, we’ll use the ‘Search Text – Regex‘ action. This action performs a regex search on a text value locating a variety of matches, for example finding all contained email addresses. Consider this flow;

When I attempt to use the data returned by the ‘Search Text – Regex‘ action, Power Automate automatically detects that the data value returned by the action is an array and therefore places the subsequent action into an ‘Apply to each‘ loop;

However, if you only want the first item in the array, you do not wish the subsequent action to be processed within an ‘Apply to each’ loop, and here is how you do it:

1. Add the action you wish to use after the action which is generating the array, but do not select any dynamic data from the action which returns the array.

2. Place your cursor where you wish to place the first item within the array, and click ‘Expression.

3. Type ‘first()’, then click ‘Dynamic content.’

4. Select the array property from dynamic data, and click ‘OK.

Expression: first(body(‘Search_Text_-_Regex’)[‘matches’])

5. Power Automate will not create an ‘Apply to each‘ loop as we have explicitly instructed the Flow to use the first item in the array. Easy!

Get the first file in a file array.

The first section of this blog helps to explain the following more complex scenario of obtaining the first file from an array of files in Power Automate. Consider the following example;

The Split PDF action returns an array of files; however, if we attempt to use a property from the variety ‘Documents Filename‘ or ‘Documents File Content‘, a loop is automatically created.

Avoiding this is a little more complex as each item in the collection is a JSON object with multiple properties. If we followed the steps outlined in the previous section, you would not be able to see the ‘Documents Filename‘ or ‘Documents File Content‘ properties within Power Automate dynamic data to select to place inside the first() expression:

The JSON value for each item in the array contains ‘fileName’ and ‘fileContent’ properties. The latter is the file Base64 encoded.

{
    "fileName":"0001_split.pdf",
    "fileContent":"JVBERi0xLjUNJcjIyMjgo8PC9UaXRsZShcMj..."
}

To obtain property values from the first item in the file array, follow these steps:

1. Add your following action beneath the action generating the array of files, the Split PDF action in this example.

2. For this example, we’ll pass the ‘fileName‘ property to the ‘File Name‘ actions property. Place your cursor, then click ‘Expression.

3. Enter the following expression, and click ‘OK.

Expression: first(outputs(‘Split_PDF’)?[‘body/documents’])?[‘fileName’]

4. Repeat for the ‘File Content’ using the following expression

Expression: base64ToBinary(first(outputs(‘Split_PDF’)?[‘body/documents’])?[‘fileContent’])

And that’s it! Only the first document of the array created by the Split PDF action will be passed to the OneDrive Create File action.

Final thoughts…

We hope you’ve found this guide useful. As ever, please share any feedback or comments. All are welcome!

Author
Jay Goodison

Managing Director

Back to all blogs

You might also be interested in...