Arrays / Looping structure – Logic Apps

Arrays / Looping structure - Logic Apps

This post covers what’s necessary to iterate over a collection of data when building workflows on schema version 2015-08-01-preview. This differs from schema 2014-12-02-preview, which can be found here. If you are still looking for the “Repeat over a list” menu item, keep on reading.

Arrays / Looping structure – What’s new

Within the release of version 2015-08-01-preview, a lot has changed when it comes to iterating over a collection data. The most noticeable thing is that the designer doesn’t include the “Repeat over a list” option. Next to this, the Workflow Definition Language has been altered to address some naming issues. This resulted in renaming Repeat to Foreach and RepeatItem to Item. In addition, the functions @repeatItem(), @repeatBody() and @repeatOutputs() have been removed.

Smart(er) designer

At this point you might ask yourself how to iterate over items when creating a Logic App workflow using the designer. Well, in most cases the new designer is smart enough to determine this on its own, just by looking at the swagger metadata exposed by the action responsible for gathering the data. Surprisingly, this works perfectly for complex types like a collection of JSON objects like the example below:

The designer understands what properties are available and will create the looping structure automatically.

Arrays-Looping-structure

List of simple types

When it comes to simple types like a JSON array of strings, booleans, integers etc. Some additional work is required. By default, the designer indicates that that there is a body, however won’t create the foreach structure.

To fix this, open the workflow in edit mode and move over to code view. Here you add "foreach": "@body('GetArrayOfStringsJson')". And make sure the body is set to @{item()} and not @item. The complete sample looks something like this; (depending on what action type you are using to consume the data)

Documentation: https://msdn.microsoft.com/en-us/library/azure/mt643789.aspx

  • Lundwa

    I ge the following message when I attempt to do a foreach on a json array “string” parse using ‘json()’:

    {“code”:”ExpressionEvaluationFailed”,”message”:”The execution of template action ‘For_each’ failed: The result ‘[{“Foo”:”Bar”}]’ of the evaluation of ‘foreach’ action expression ‘@{json(decodeBase64(triggerBody()[‘ContentData’]))}’ is not a valid array.”}

    • Hi Lundwa,

      Is [{“Foo”:”Bar”}] the original format of your nameless Json array or has it been altered in the process of displaying a friendly error message?

      I’ve written a detailed post on how to iterate over Json data within your logic app which can be found here. http://devslice.net/2016/03/parsing-json-data-within-logic-apps/

      When iterating over a nameless json array like [{“Foo”:”Bar”}], just take away .employees from the foreach condition as shown within the code samples.

Post Navigation