Tous les Technical posts

Working with collections in Logic Apps

Quite often I get questions on how to modify JSON collections or arrays within Logic Apps. Before reaching out to Azure Functions, as an extensibility option, I prefer to use the out-of-the-box available Logic Apps functionality. This blog post contains some tips and tricks about dealing with collections.

Remove and rename properties of items within a collection

This example explains how you can easily remove and rename properties within the repeated items of a collection. Therefore, we leverage the Data Operations – Select action. Below you can find a simple example.

Input collection

This is the sample input collection.

[
{
"OrderId" : "1",
"Product" : "Consultancy",
"Quantity" : "16",
"Price" : "100"
},
{
"OrderId" : "2",
"Product" : "Training",
"Quantity" : "1000",
"Price" : "150"
},
{
"OrderId" : "3",
"Product" : "Development",
"Quantity" : "80",
"Price" : "75"
}
]
view raw Input_1 hosted with ❤ by GitHub

Select action

Configure the Data Operations – Select action, so only two properties remain, while being renamed. The output array will contain the same number of items, whilst the items will only include Item and OrderNumber as properties. This offers a great user experience for performing simple transformations on collections.
Collections 1

Output Collection

This is the resulting output collection.

[
{
"Item": "Consultancy",
"OrderNumber": "1"
},
{
"Item": "Training",
"OrderNumber": "2"
},
{
"Item": "Development",
"OrderNumber": "3"
}
]
view raw Output_1 hosted with ❤ by GitHub

Reduce the number of items within a collection

This sample explains how you can easily reduce the number of items within a collection. The following expressions can be used to perform this job, without looking at the content of the item:

  • @first: returns the first item of the collection
  • @last: returns the last item of the collection
  • @take: returns the first x items of the collection
  • @skip: returns the last x items of the collection

In case you want to reduce the number of items in a collection, based on their content, you can use the Data Operations – Filter action. Below you can find a simple example.

Input collection

This is the sample input collection.

[
{
"OrderId" : "1",
"Product" : "Consultancy",
"Quantity" : "16",
"Price" : "100"
},
{
"OrderId" : "2",
"Product" : "Training",
"Quantity" : "1000",
"Price" : "150"
},
{
"OrderId" : "3",
"Product" : "Development",
"Quantity" : "80",
"Price" : "75"
}
]
view raw Input_2 hosted with ❤ by GitHub

Filter action

Configure the Data Operations – Filter action, so only the items that have a quantity lower than or equal to 100, are included in the output collection. This allows you to quickly take a subset of a collection, based on specific filter criteria. With the available Logic Apps expressions, you can write quite complex and powerful filter queries.
Collections 2

Output Collection

This is the resulting output collection.

[
{
"OrderId": "1",
"Product": "Consultancy",
"Quantity": "16",
"Price": "100"
},
{
"OrderId": "3",
"Product": "Development",
"Quantity": "80",
"Price": "75"
}
]
view raw Output_2 hosted with ❤ by GitHub

Remove duplicate items from a collection

This section explains how you can easily remove duplicate items from a collection. You can use the @union expression to achieve this, although it’s not originally designed for that. Below you can find a simple example.

Input collection

This is the sample input collection.

[
{
"OrderId" : "1",
"Product" : "Consultancy",
"Quantity" : "16",
"Price" : "100"
},
{
"OrderId" : "3",
"Product" : "Development",
"Quantity" : "80",
"Price" : "75"
},
{
"OrderId" : "3",
"Product" : "Development",
"Quantity" : "80",
"Price" : "75"
}
]
view raw Input_3 hosted with ❤ by GitHub

Compose action

Configure a Data Operations – Compose action that uses the @union expression. This expression merges the content of two input collections, but has also a side-effect that only one of the duplicate items is included in the output collection. So, as a trick, you can use @union(triggerBody(),triggerBody()) to remove duplicate items from a collection. I pass the input collection twice to the @union expression, cause I didn’t find out how to pass an empty collection.
Collections 3

If you don’t want any of the duplicate items to be included in the output collection, you could use the reverse @intersection expression in a similar way.

Output Collection

This is the resulting output collection, using the @union expression.

[
{
"OrderId": "1",
"Product": "Consultancy",
"Quantity": "16",
"Price": "100"
},
{
"OrderId": "3",
"Product": "Development",
"Quantity": "80",
"Price": "75"
}
]
view raw Output_3 hosted with ❤ by GitHub

Conclusion

Logic Apps has already several powerful mechanisms to modify collections in a user friendly way. The product team is constantly adding enhancements to the framework, so I’m convinced that we will enjoy additional ways to handle arrays in the near future!

Do you also know a trick about handling Logic App collections? Don’t hesitate to share it in the comments section below! Are you looking for more info on transforming JSON objects in Logic Apps? Definitely have a look over here!

Thank you
Toon

S'abonner au flux RSS

Bonjour,
comment pouvons-nous vous aider?

Envie de travailler avec nous?

Entrons en contact

Let's talk

Let's talk

Merci, nous revenons vers vous au plus vite!

Appelez-nous

Merci, nous avons envoyé le Témoignage dans votre boîte de réception

Adresse e-mail invalide

Envoyer

Le téléchargement devrait démarrer dans quelques secondes!

Restons en contact - Abonnez-vous à notre newsletter

Tendances de l’industrie, événements et cas clients

Adresse e-mail invalide

Envoyer

Parfait, vous voilà inscrit!