This post was originally published here.
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" } ]
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.
Output collection
This is the resulting output collection.
[ { "Item": "Consultancy", "OrderNumber": "1" }, { "Item": "Training", "OrderNumber": "2" }, { "Item": "Development", "OrderNumber": "3" } ]
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" } ]
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.
Output collection
This is the resulting output collection.
[ { "OrderId": "1", "Product": "Consultancy", "Quantity": "16", "Price": "100" }, { "OrderId": "3", "Product": "Development", "Quantity": "80", "Price": "75" } ]
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" } ]
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.
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" } ]
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