Free Trial Book a Demo

Logic App Best Practices, Tips, and Tricks: #38 How to get distinct values from an array?

4 Mins Read | Last modified on November 29th, 2023

get distinct values from an array in logic apps

Welcome again to another Logic Apps Best Practices, Tips, and Tricks. In my previous blog posts, I discussed some of the essential best practices you should have while working with the Azure Logic App.

Today, I will speak about another helpful Best Practice, Tips, and Tricks that you must consider while designing your business processes (Logic Apps): How do we get distinct values from an array or repeating structure?
Get distinct values from an array in logic apps

How do we get distinct values from an array or repeating structure?

Finding unique values, or distinct values like an ID inside an array of objects or in a repeating JSON or XML structure, is a usual requirement in integration projects. However, this simple and common task can present a challenge inside Logic Apps. Of course, like everything in the world, you may find several ways to accomplish that, like:

  • Using an Azure Function to clean repeating values or structures or find unique values inside the structure.
  • Or using an XSLT or liquid map to accomplish the same goal.

But what would be the simple and useful way – in a no-code, low-code approach – to accomplish that?

The answer may be strange, but it is quite simple: using the union(collection, collection2) expression by comparing the same collection (array)!

First, let’s see what the union(collection, collection) expression tells us:

  • Based on the official documentation, the union expression returns a collection containing all the items from the specified collections. To appear in the result, an item can appear in any collection passed to this function. If one or more items have the same name, the last item with that name appears in the result.
  • As an example, the following expression will return [1, 2, 3, 10, 101].
union(createArray(1, 2, 3), createArray(1, 2, 10, 101))

Taking this approach in a more real exercise, where we have the next JSON payload:

   "cars" : [
         "id": 1,
         "name": "Mercedes C350e"
         "id": 2,
         "name": "Toyota Avensis "
         "id": 1,
         "name": "Mercedes C350e"

We want to extract/get all the IDs from a JSON structure into an array – get distinct IDs. We could accomplish this by using a:

  • Data Operations > Select action
  • followed by a union(…) expression inside any action.

Let’s test this with a small proof-of-concept:

  • Create an empty Logic App from the Azure Portal (or the editor of your choice)
  • Add a Request > When a HTTP request is received trigger to our blank Logic App and set the Request Body JSON Schema property by providing the above JSON request as a sample payload to generate schema.

Create an empty Logic App and add a HTTP request

  • Now add a Data Operations > Select action and configure the action to be:
    • On the From property set the repeating node, in our sample, cars.
    • On the Map table, set the following line:
      • Column 1: id (static value)
      • Column 2: @item()?[‘id’] (id field from the cars structure)

Add Data Operations and configure them

  • Note: the result of the Data Operations > Select action will be the following object array:
    • id:1
    • id:2
    • id:1
  • Now, if we add a Request > Response action and configure the response as:
    • On the Status Code property set as 200.
    • On the Headers table set:
      • Context-Type: application/json
    • On the Body property set to be the following expression

get distinct values from an array in logic apps

Note that this will perform a union (and simultaneously compare the values) of a structure against the same structure!  The end result will be an array with distinct values. To simplify this scenario, the expression will be kind of:

union([1, 2, 1], [1, 2, 1])

Ad the result will end up being [1, 2] – And if you see, these are the distinct IDs!

If we test this POC with Postman, the end result will be:

        "id": 1
        "id": 2

get distinct values from an array in logic apps

I hope you enjoy this developer tip and stay tuned for the following Logic App Best practices, Tips, and Tricks.

This article was originally published on Nov 2, 2023. It was most recently updated on Nov 29, 2023.

Azure Logic App - Request Demo CTA
Need a better Azure Management platform?

Serverless360 helps to streamline Azure monitoring, distributed tracing, documentation and optimize cost.

Sign up Now
Monitoring Azure Functions Effortlessly