all 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.

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.

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.

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.

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.

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.

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

Subscribe to our RSS feed

Thanks, we've sent the link to your inbox

Invalid email address

Submit

Your download should start shortly!

Stay in Touch - Subscribe to Our Newsletter

Keep up to date with industry trends, events and the latest customer stories

Invalid email address

Submit

Great you’re on the list!