Today I was asked about how arrays work in Microsoft Flow. In this post I will go through many of the common scenarios that you may face with arrays in Microsoft Flow.


I will start with creating arrays, then I will look at manipulating arrays, but first of all what are arrays?

In most common programming languages you will recognise the term array as a block of related data elements. You could see this as a table or a list list alike data structure.

Creating arrays

I’m going to start with the following table of data.

Brand Colour Age Registration Date
Nissan Red 3 01/07/2015
Citroen Green 2 21/09/2016
Audi Blue 4 01/01/2014

I want to create an array like the above table.

In flow I could now create an array using the createArray function.

createArray('Nissan','Citroen','Audi')

 

I could even take this further

createArray(createArray('Nissan','Red',3,'01/07/2015'),createArray('Ctiroen','Green',2,'21/09/2016'),createArray('Audi','Blue',4,'01/07/2014'))

And I will get an array of arrays

[
[
"Nissan",
"Red",
3,
"01/07/2015"
],
[
"Ctiroen",
"Green",
2,
"21/09/2016"
],
[
"Audi",
"Blue",
4,
"01/07/2014"
]
]

Where do I find arrays?

Within Flow you can find arrays everywhere. The most common place and most visible place is within the Apply to each step. In this step flow will take an array and step through the elements in the array. So If we take a further look at the previous example then a Compose delivering an array can split by an Apply to each step:

Running through the Apply to each you will find the separate elements of my array.

So far this post is all about creating arrays and then stepping through arrays. Now that we have some basic understanding of arrays in Flow it is time to get some real arrays. For this I’m going to create a list in SharePoint with the same data as in the above table.

Using the Get Items action from the SharePoint connector I can now read the data form this list.

Looking at the Body of the GetItems action you will see somethign like this:

{
"value": [
{
"@odata.etag": "\"1\"",
"ItemInternalId": "1",
"ID": 1,
"Title": "Nissan",
"Colour": {
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
"Id": 0,
"Value": "Red"
},
"Colour#Id": 0,
"Age": 3,
"Registration_x0020_Date": "2015-01-07",
"Modified": "2018-07-10T08:36:27Z",
"Created": "2018-07-10T08:36:27Z",
"Author": {
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
"Claims": "i:0#.f|membership|pieter@pieterveenstratriaddev.onmicrosoft.com",
"DisplayName": "Pieter Veenstra",
"Email": "pieter@PieterVeenstraTriadDev.onmicrosoft.com",
"Picture": "https://pieterveenstratriaddev.sharepoint.com/_layouts/15/UserPhoto.aspx?Size=L&AccountName=pieter@PieterVeenstraTriadDev.onmicrosoft.com",
"Department": null,
"JobTitle": null
},
"Author#Claims": "i:0#.f|membership|pieter@pieterveenstratriaddev.onmicrosoft.com",
"Editor": {
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
"Claims": "i:0#.f|membership|pieter@pieterveenstratriaddev.onmicrosoft.com",
"DisplayName": "Pieter Veenstra",
"Email": "pieter@PieterVeenstraTriadDev.onmicrosoft.com",
"Picture": "https://pieterveenstratriaddev.sharepoint.com/_layouts/15/UserPhoto.aspx?Size=L&AccountName=pieter@PieterVeenstraTriadDev.onmicrosoft.com",
"Department": null,
"JobTitle": null
},
"Editor#Claims": "i:0#.f|membership|pieter@pieterveenstratriaddev.onmicrosoft.com",
"{Identifier}": "Lists%252fCars%252f1_.000",
"{Link}": "https://pieterveenstratriaddev.sharepoint.com/_layouts/15/listform.aspx?PageType=4&ListId=51c5fad6-6c45-4bc4-8632-556674abe0be&ID=1&ContentTypeID=0x01004819843C002BE14D97CADA4CCA608281",
"{Name}": "Nissan",
"{FilenameWithExtension}": "Nissan",
"{Path}": "Lists/Cars/",
"{HasAttachments}": false
},
{
"@odata.etag": "\"1\"",
"ItemInternalId": "2",
"ID": 2,
"Title": "Citroen",
"Colour": {
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
"Id": 2,
"Value": "Green"
},
"Colour#Id": 2,
"Age": 2,
"Registration_x0020_Date": "2016-09-23",
"Modified": "2018-07-10T08:37:04Z",
"Created": "2018-07-10T08:37:04Z",
"Author": {
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
"Claims": "i:0#.f|membership|pieter@pieterveenstratriaddev.onmicrosoft.com",
"DisplayName": "Pieter Veenstra",
"Email": "pieter@PieterVeenstraTriadDev.onmicrosoft.com",
"Picture": "https://pieterveenstratriaddev.sharepoint.com/_layouts/15/UserPhoto.aspx?Size=L&AccountName=pieter@PieterVeenstraTriadDev.onmicrosoft.com",
"Department": null,
"JobTitle": null
},
"Author#Claims": "i:0#.f|membership|pieter@pieterveenstratriaddev.onmicrosoft.com",
"Editor": {
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
"Claims": "i:0#.f|membership|pieter@pieterveenstratriaddev.onmicrosoft.com",
"DisplayName": "Pieter Veenstra",
"Email": "pieter@PieterVeenstraTriadDev.onmicrosoft.com",
"Picture": "https://pieterveenstratriaddev.sharepoint.com/_layouts/15/UserPhoto.aspx?Size=L&AccountName=pieter@PieterVeenstraTriadDev.onmicrosoft.com",
"Department": null,
"JobTitle": null
},
"Editor#Claims": "i:0#.f|membership|pieter@pieterveenstratriaddev.onmicrosoft.com",
"{Identifier}": "Lists%252fCars%252f2_.000",
"{Link}": "https://pieterveenstratriaddev.sharepoint.com/_layouts/15/listform.aspx?PageType=4&ListId=51c5fad6-6c45-4bc4-8632-556674abe0be&ID=2&ContentTypeID=0x01004819843C002BE14D97CADA4CCA608281",
"{Name}": "Citroen",
"{FilenameWithExtension}": "Citroen",
"{Path}": "Lists/Cars/",
"{HasAttachments}": false
},
{
"@odata.etag": "\"1\"",
"ItemInternalId": "3",
"ID": 3,
"Title": "Audi",
"Colour": {
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
"Id": 1,
"Value": "Blue"
},
"Colour#Id": 1,
"Age": 4,
"Registration_x0020_Date": "2014-01-01",
"Modified": "2018-07-10T08:37:42Z",
"Created": "2018-07-10T08:37:42Z",
"Author": {
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
"Claims": "i:0#.f|membership|pieter@pieterveenstratriaddev.onmicrosoft.com",
"DisplayName": "Pieter Veenstra",
"Email": "pieter@PieterVeenstraTriadDev.onmicrosoft.com",
"Picture": "https://pieterveenstratriaddev.sharepoint.com/_layouts/15/UserPhoto.aspx?Size=L&AccountName=pieter@PieterVeenstraTriadDev.onmicrosoft.com",
"Department": null,
"JobTitle": null
},
"Author#Claims": "i:0#.f|membership|pieter@pieterveenstratriaddev.onmicrosoft.com",
"Editor": {
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
"Claims": "i:0#.f|membership|pieter@pieterveenstratriaddev.onmicrosoft.com",
"DisplayName": "Pieter Veenstra",
"Email": "pieter@PieterVeenstraTriadDev.onmicrosoft.com",
"Picture": "https://pieterveenstratriaddev.sharepoint.com/_layouts/15/UserPhoto.aspx?Size=L&AccountName=pieter@PieterVeenstraTriadDev.onmicrosoft.com",
"Department": null,
"JobTitle": null
},
"Editor#Claims": "i:0#.f|membership|pieter@pieterveenstratriaddev.onmicrosoft.com",
"{Identifier}": "Lists%252fCars%252f3_.000",
"{Link}": "https://pieterveenstratriaddev.sharepoint.com/_layouts/15/listform.aspx?PageType=4&ListId=51c5fad6-6c45-4bc4-8632-556674abe0be&ID=3&ContentTypeID=0x01004819843C002BE14D97CADA4CCA608281",
"{Name}": "Audi",
"{FilenameWithExtension}": "Audi",
"{Path}": "Lists/Cars/",
"{HasAttachments}": false
}
]
}

This is where the data in Microsoft Flow can become difficult to understand. The above JSON might need a second look before you really understand what data can be found. Flow is making your life easy here! Use dynamic  content and you simply get your SharePoint list columns back.  It even help you with the complexities around lookups,  dates  and other column types.

Time for a step into the deep end.  There is a lot more we can do with arrays. I’m going to start with the arrays functions available within Microosft Flow. You will notice that in the referenced article the term Collections is also used.

Collection function Task
contains Check whether a collection has a specific item.
empty Check whether a collection is empty.
first Return the first item from a collection.
intersection Return a collection that has only the common items across the specified collections.
join Return a string that has all the items from an array, separated by the specified character.
last Return the last item from a collection.
length Return the number of items in a string or array.
skip Remove items from the front of a collection, and return all the other items.
take Return items from the front of a collection.
union Return a collection that has all the items from the specified collections.

Hey, I want to do more! Where is the sort function?

Well remember when we got the data from my SharePoint list?  In the Get Items action you can sort your data. So you might want to sort your data when you collect your data.

Also in the Data operations connector there is no sort:

 

The examples

Contains

The contains functions checks your array for any values or elements in your collection or array.

Looking at the following 3 examples. The first 2 return false where the last one returns true.

contains(body('Get_items')?['Value'],'Nissan')

contains(outputs('Compose'),'Nissan')

contains(first(outputs('Compose')),'Nissan')

Within the last example I selected a single record form my array before checking with the contains function for my car brand. Where in the first two examples I’m supplying an array of array to the contains function.

Empty

Empty checks the length of the Collection if no items are found in the array then true is returned. Note that this is not the same as comparing an array to a null value.

First

The First functions returns the first item form the collection. See the 3rd example from the contains section above for a real example.

Intersection

Imagine that you have two arrays and you want to get the items that exist in both collection. and Now you want to find the items that exist in both collections.

So you could for example have two lists with cars. One list is called my cars and one is called insured cars if you now want to find out which of your cars are insured you could use the intersection function.

intersection(body('Get_items')?['Value'],body('Get_items_2')?['Value'])

Join

When you have an array of strings and you would like to create a character separated list of strings then you could do this with the join function. Have you ever has a list of users in SharePoint and you wanted to email them all?

join(outputs('Compose'),';')

Last

The last function is similar the the first function, other than that you get the last element in the array rather than the first one.

Length

The length function gives you the number of elements in a collection. Nothing exciting here.

Skip

With the skip function you can select all elements after a certain position.

skip(outputs('Compose'),2)

Take

The take function is simialr to the skipp fucntion but now you can collect all the elements up to a certain point in your array.

take(outputs('Compose'),2)

Union

In my earlier examples I had two arrays. One that I manually created and  and one that I get from the Get Items action. To merge these arrays into one array you could run the following:

union(body('Get_items')?['Value'],outputs('Compose'))

Did you notice that Flow really doesn’t care that these two arrays are not exactly the same. As Flow will just consider these two arrays to be JSON. It is just gluing the two collections of data together. this is where a sort might be important, although most of the times you will find that you will just want to process each element in your collection and the order really doesn’t matter.

Select

The select action I also want to include here. Although Select is not a function within Microsoft Flow it can help you transform an array of elements.

 

Challenge of the week

As I mentioned a few time in the article there is no sort of array elements. Can you implement quick sort in flow?

 

Advertisements