What is this?

Logic apps are great. However, they can be too great. Because they’re effectively a WYSIWYG wizard-style editor, they try and do everything for you.

This can become tricky when you’re trying to work with complex data schema, and do things like iterate over data or specify certain condition matching to work with subsets of data.

Let’s take a look some high level concepts, and how you can easily work with data in logic apps.

TL:DR at bottom.

First things first - data

As per usual, you need to make sure your data is preprocessed correctly. Use JSON. Even so, make sure the block of data you’re trying to iterate over is an array. It’s a common mistake to try to iterate over a JSON object, which cannot be performed natively.

Array = list of things you can do stuff with and index into
Object = thing which has properties which you can reference by property name

For example:

Something which you can iterate over - array

See below, I have an array of pets. Lets say I have 2 pets, 2x dogs to be specific . The array contains 2x JSON objects, and each object (pet) has properties associated with it

{
    "pets": [
        {
            "name": "Fido",
            "type": "dog",
            "breed": "Finnish Lapphund"
        },
        {
            "name": "Cinnamon",
            "type": "dog",
            "breed": "Golden Retriever"
        }
    ]
}

source: reddit

Something you can NOT iterate over, or index into

See below, this is an example of completely incorrect JSON which doesn’t actually meet the official spec:

{
    "Pets": {
        {
            "name": "Fido",
            "type": "dog",
            "breed": "Finnish Lapphund"
        },
        {
            "name": "Cinnamon",
            "type": "dog",
            "breed": "Golden Retriever"
        }
    }
}
Lesson: know what you're working with

But what about <insert other data format here, probably XML>?

Take your XML and other data formats and throw them into the bin. It’s much easier and cheaper with JSON. Use your method of choice to convert to JSON, you can use Azure Functions to do this very cheaply, or an integration account with your logic apps.

There’s a particular integration pattern called the mapper pattern which resolves this, using a tool of your choice.

Second things second (?) - apply your correct schema as input

When creating what would be a HTTP trigger, paste some example schema into the logic app HTTP trigger. This makes your life 1000 times easier, as per my first point the logic app editor tries to figure everything out for you, and it all starts here:

Perform small, incremental steps when designing the logic app

Don’t try use 63 steps at once before running your logic app for the first time. In the example for this post, I’ve just taken the input and created a response to validate the logic app. Start small, add a step, test, rinse-repeat:

easy peasy

OK cool now what

By now we’re up to working with the data, and getting our hands dirty with the logic app code syntax. This is where things generally fall over.

The syntax generally goes something like this:

ReferenceToStepWeWantToUse().property.subproperty.subsubproperty.etc

For ‘ReferenceToStepWeWantToUse’, there’s several out-of-the-box things we can use:

  • Trigger
  • Items (in a for each loop)
  • Automatic calculated variables from previous steps

See the below, this says “Take our trigger, the output we received, use the body which was POSTed, and use the ‘pets’ property”

NOTE: I manually am typing that code, and there’s an ‘@’ at the front. So it looks like this:

If correct, once you click out/save, it will turn into the nice colourful box. If it doesn’t click into the code editor and look at the statement - it will usually have an extra “@” at the front:

Remove the extra “@”.

We can then take our pets array, and iterate over it. Let’s do that, referencing the ‘breed’ property. The syntax is the exact same, but we reference 'items('Loop_Step_Name') as the function:

This says “For each item in this loop, use the ‘breed’ property”

From here, you can do whatever, I’ve crafted a simple response which is an array of the breeds:

How do we match on a condition?

Super easy, there’s an in-built function for condition matching. In the below example, we match if the word “Finnish” is detected in breed:

But what about really complex data types?

The above example is easy. We took a well formatted JSON object with an array, iterated over the items, plucked a property, appended it to a variable and responded.

Let’s extend our schema somewhat, as our pets have favourite food:

    {
        "name": "Fido",
        "type": "dog",
        "breed": "Finnish Lapphund",
        "favouriteFoods": [
            {
                "make": "meat",
                "model": "chicken",
                "suppliers": [
                    {
                        "name": "coles",
                        "suburb": "brisbane"
                    }
                ]
            },
            {
                "make": "meat",
                "model": "ham",
                "suppliers": [
                    {
                        "name": "butcher",
                        "suburb": "albion"
                    }
                ]
            }
        ]
    }

How do we find food which can be bought from coles?

TL;DR

From our simple example to our more complex nested example, the concept is the exact same:

1. Keep referencing properties by name, until you hit an array
2. Iterate over the array, using above condition logic if you want
3. Go to step 1

It simply comes down to having an understanding about your data, and a consistent schema.