We provide three separate split actions for Power Automate which allow you to split a PDF document using a myriad of 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 array only contains one document it is still an array and accessing the document without an ‘Apply to each’ loop is a common question which 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 an array 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 really want 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, click ‘Expression

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

4. Select the array property from dynamic data, 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 really helps to explain the next 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 array ‘Documents Filename‘ or ‘Documents File Content‘ a loop is automatically created.

Avoiding this with a file array is a little more complex as each item in the array is actually a JSON object with multiple properties and 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, 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.