This blog will brief on Azure Logic App Expressions. Before getting deep into Logic App expressions let us have a short introduction about Workflow Definition Language in Azure Logic Apps.
Workflow Definition Language
Whenever a Logic App is created, it has an underlying workflow definition that describes the actual logic that runs in the Logic App. That workflow definition uses JSON and follows a structure that is validated by the Workflow Definition Language schema.
Workflow Definition Structure
The workflow definition includes a trigger for instantiating the Logic App and one or more actions that run after the trigger fires.
The high-level structure of a workflow definition would look like the one below:
"definition": {
"$schema": "
"actions": { "
"contentVersion": "
"outputs": { "
"parameters": { "
"staticResults": { "
"triggers": { "
}
More details on workflow definition can be found here.
Expressions
The workflow definition is represented in JSON form. So, it can have literal (static) values that exist at the time of design, for instance:
"organizationName": "Kovai.co",
"products": ["BizTalk360", "Turbo360", "Document360", "AtomicScope”],
"productsCount": 4
It can also have values that do not exist until run time. To represent these values, expressions are used, which are evaluated at run time.
An expression is a sequence that contains one or more functions, operators, variables, explicit values, or constants. In a workflow definition, expression can be used anywhere in a JSON string value by prefixing the expression with the at-sign ‘@’. When evaluating an expression that represents a JSON value, the expression body is extracted by removing the ‘@’ character. It always results in another JSON value.
For instance, to assign a value to a property ‘parameters()’ function can be used. Parameters is a section in workflow definition, where some values used by Logic App at runtime are stored. We will investigate on Logic App Function expression later in this blog.
“customerName”: "@parameters('customerName')"
Multiple expressions can be joined by ‘String Interpolation’ that are wrapped by ‘@’ character and curly braces ({}). It can be represented as below,
"customerName": "@{parameters('firstName')}_@{parameters('lastName')}"
The result is always a string, the result of the above expression is,
"customerName": “Alex_Bryan"
When working with Logic App Designer, expressions can be created through the Expression builder, once done the expression will appear for the corresponding property in the workflow definition.
Functions in Expression for Azure Logic Apps
Some expressions in the Workflow definition of Logic App get its values from runtime actions which may not yet exist when your workflow starts running. To get these values or process the values in these expressions, functions provided by workflow definition language can be used.
The general-purpose functions that are available are:
String Functions
String functions are used to work with strings. These functions work only on strings. The string functions available are:
- concat – Combine two or more strings and return the combined string.
- endsWith – Check whether a string ends with the specified substring.
- guid – Generate a globally unique identifier (GUID) as a string.
- indexOf – Return the starting position for a substring.
- lastIndexOf – Return the starting position for the last occurrence of a substring.
- replace – Replace a substring with the specified string and return the updated string.
- split – Return an array that contains substrings, separated by commas, from a larger string based on a specified delimiter character in the original string.
- startsWith – Check whether a string starts with a specific substring.
- substring – Return characters from a string, starting from the specified position.
- toLower – Return a string in lowercase format.
- toUpper – Return a string in uppercase format.
- trim – Remove leading and trailing whitespace from a string and return the updated string.
Collection Functions
Collection functions are used to work with arrays, strings, dictionaries. The collection functions available are:
- contains – check whether a collection has a specific item.
- empty – Check whether a collection is empty.
- first – Return the first item from a collection.
- intersection – Return a collection that has only the common items across the specified collections.
- item – When inside a repeating action over an array, return the current item in the array during the action’s current iteration.
- join – Return a string that has all the items from an array, separated by the specified character.
- last – Return the last item from a collection.
- length – Return the number of items in a string or array.
- skip – Remove items from the front of a collection and return all the other items.
- take – Return items from the front of a collection.
- union – Return a collection that has all the items from the specified collections.
Logical Comparison Functions
Logical comparison functions are used to work with conditions, compare values and expression results, or evaluate various kinds of logic. The logical comparison functions available are:
- and – Check whether all expressions are true.
- equals – Check whether both values are equivalent.
- greater – Check whether the first value is greater than the second value.
- greaterOrEquals – Check whether the first value is greater than or equal to the second value.
- if – Check whether an expression is true or false. Based on the result, return a specified value.
- less – Check whether the first value is less than the second value.
- lessOrEquals – Check whether the first value is less than or equal to the second value.
- not – Check whether an expression is false.
- or – Check whether at least one expression is true.
Conversion Functions
Conversion functions are used to change a value’s type or format. The conversion functions available are:
- array – Return an array from a single specified input.
- base64 – Return the base64-encoded version for a string.
- base64ToBinary – Return the binary version for a base64-encoded string.
- base64ToString – Return the string version for a base64-encoded string.
- binary – Return the binary version for an input value.
- bool – Return the Boolean version for an input value.
- createArray – Return an array from multiple inputs.
- dataUri – Return the data URI for an input value.
- dataUriToBinary – Return the binary version for a data URI.
- dataUriToString – Return the string version for a data URI.
- decodeBase64 – Return the string version for a base64-encoded string.
- decodeDataUri – Return the binary version for a data URI.
- decodeUriComponent – Return a string that replaces escape characters with decoded versions.
- encodeUriComponent – Return a string that replaces URL-unsafe characters with escape characters.
- float – Return a floating-point number for an input value.
- int – Return the integer version for a string.
- json – Return the JavaScript Object Notation (JSON) type value or object for a string or XML.
- string – Return the string version for an input value.
- uriComponent – Return the URI-encoded version for an input value by replacing URL-unsafe characters with escape characters.
- uriComponentToBinary – Return the binary version for a URI-encoded string.
- uriComponentToString – Return the string version for a URI-encoded string.
- xml – Return the XML version for a string.
Math Functions
Math functions are used to work with integers and floats. The math functions available are:
- add – Return the result from adding two numbers.
- div – Return the result from dividing two numbers.
- max – Return the highest value from a set of numbers or an array.
- min – Return the lowest value from a set of numbers or an array.
- mod – Return the remainder from dividing two numbers.
- mul – Return the product from multiplying two numbers.
- rand – Return a random integer from a specified range.
- range – Return an integer array that starts from a specified integer.
- sub – Return the result from subtracting the second number from the first number.
Date and Time Functions
Date and time functions are used to work with dates and times. Date and time functions available are:
- addDays – Add several days to a timestamp.
- addHours – Add several hours to a timestamp.
- addMinutes – Add several minutes to a timestamp.
- addSeconds – Add several seconds to a timestamp.
- addToTime – Add several time units to a timestamp.
- convertFromUtc – Convert a timestamp from Universal Time Coordinated (UTC) to the target time zone.
- convertTimeZone – Convert a timestamp from the source time zone to the target time zone.
- convertToUtc – Convert a timestamp from the source time zone to Universal Time Coordinated (UTC).
- dayOfMonth – Return the day of the month component from a timestamp.
- dayOfWeek – Return the day of the week component from a timestamp.
- dayOfYear – Return the day of the year component from a timestamp.
- formatDateTime – Return the date from a timestamp.
- getFutureTime – Return the current timestamp plus the specified time units
- getPastTime – Return the current timestamp minus the specified time units.
- startOfDay – Return the start of the day for a timestamp.
- startOfHour – Return the start of the hour for a timestamp.
- startOfMonth – Return the start of the month for a timestamp.
- subtractFromTime – Subtract several time units from a timestamp.
- ticks – Return the ticks property value for a specified timestamp.
- utcNow – Return the current timestamp as a string.
Workflow Functions
Workflow functions are used to get details about a workflow instance at run time, work with the inputs used for instantiating Logic Apps or Flows, reference the outputs from triggers and actions. Workflow functions available are:
- action – Return the current action’s output at runtime, or values from other JSON name-and-value pairs.
- actionBody – Return an action’s body output at runtime.
- actionOutputs – Return an action’s output at runtime.
- actions – Return an action’s output at runtime, or values from other JSON name-and-value pairs.
- body – Return an action’s body output at runtime.
- formDataMultiValues – Create an array with the values that match a key name in form-data or form-encoded action outputs.
- formDataValue – Return a single value that matches a key name in an action’s form-data or form-encoded output.
- item – When inside a repeating action over an array, return the current item in the array during the action’s current iteration.
- items – When inside a Foreach or Until loop, return the current item from the specified loop.
- iterationIndexes – When inside an Until loop, return the index value for the current iteration. You can use this function inside nested Until loops.
- listCallbackUrl – Return the “callback URL” that calls a trigger or action.
- multipartBody – Return the body for a specific part in an action’s output that has multiple parts.
- outputs – Return an action’s output at runtime.
- parameters – Return the value for a parameter that is described in your workflow definition.
- result – Return the inputs and outputs from all the actions inside the specified scoped action, such as For_each, Until, and Scope.
- trigger – Return a trigger’s output at runtime, or from other JSON name-and-value pairs.
- triggerBody – Return a trigger’s body output at runtime
- triggerFormDataValue – Return a single value matching a key name in form-data or form-encoded trigger outputs.
- triggerMultipartBody – Return the body for a specific part in a trigger’s multipart output.
- triggerFormDataMultiValues – Create an array whose values match a key name in form-data or form-encoded trigger outputs.
- triggerOutputs – Return a trigger’s output at runtime, or values from other JSON name-and-value pairs.
- variables – Return the value for a specified variable.
- workflow – Return all the details about the workflow itself during run time.
URI Parsing Functions
URI parsing functions are used to work with uniform resource identifiers (URIs) and get various property values for these URIs. The URI parsing functions available are:
- uriHost – Return the host value for a URI.
- uriPath – Return the path value for a URI.
- uriPathAndQuery – Return the path and query values for a URI.
- uriPort – Return the port value for a URI.
- uriQuery – Return the query value for a URI.
- uriScheme – Return the scheme value for a URI.
Manipulation functions: JSON & XML
Manipulation functions are used to work with JSON objects and XML nodes. The manipulation functions available are:
- addProperty – Add a property and its value, or name-value pair, to a JSON object, and return the updated object.
- coalesce – Return the first non-null value from one or more parameters.
- removeProperty – Remove a property from a JSON object and return the updated object.
- setProperty – Set the value for a JSON object’s property and return the updated object.
- xpath – Check XML for nodes or values that match an XPath (XML Path Language) expression and return the matching nodes or values.
For more information and examples of the Azure Logic App Function Expressions refer here.
A Scenario where Expressions can be used
Consider a scenario of an invoice system that is represented by a Logic App. When a new e-mail containing the order and invoice information arrives, a message is pushed into a Service Bus Queue, which in turn will be processed by an Azure Function. Here the expressions come into picture when pushing the message into the Service Bus Queue.
When pushing a message into the queue, some properties of the mail should be manipulated.
The ‘Session Id’ of the message is set with the name of the sender of the mail so that the messages from the same sender can be grouped. The ‘From’ property from the mail is manipulated to get the name of the sender trimming the extra characters after ‘@’
substring(triggerBody()?['From'],0,indexOf(triggerBody()?['From'],'@'))
The ‘Message Id’ of the message is set with a new GUID using the function expression ‘guid()’. Few custom properties like ‘CreatedAt’ and ‘OrderDetails’ are set to the message which takes its values from function expression ‘utcNow()’ and body of the mail. An Azure Function, in turn, will parse the properties of the message and take Order details.
h
Conclusion
In this blog post, we have seen a detailed view on the Logic App Expressions and the Workflow Definition Language in Azure Logic Apps. Stay updated for more content on Azure integration space. Happy Learning!