This post was originally published here

Azure Logic Apps offer flexibility in how you access data from JSON inputs, supporting both dot and bracket notations

Azure Logic Apps offers a flexible and first-class experience for you to access data within JSON structures. The most common way to access it is using tokens when you tokenize a message using a JSON schema or by using bracket notations. In fact, tokens use behind-the-scene bracket notations to reference those values. But do you know that there is another way? Another convention?

Consider we receive the following JSON input in a Logic App through a When a HTTP Request is received trigger:

{
    "ID": {
        "name": "Luis",
        "lastName": "Rigueira",
        "age": 34
    }
}

The approach to access the name using the bracket notation would be using the expression:

triggerBody()?['ID']?['name']

In a Logic Apps context:

Now, what a lot of people don’t know, and what isn’t very common to see, is that Logic Apps also supports Dot notation. Taking the sample above, accessing the same field can be achieved in Dot notation like this:

triggerBody().ID.name

In a Logic Apps context:

As you can see both methods return the same output.

The dot notation feels more natural and readable, so why do we not use it more often?

Dot notation is notably more streamlined and mirrors the property access seen in programming languages like JavaScript, enhancing readability. Bracket notation, however, brings a level of versatility to the table.

While both notations are valid and can be used interchangeably in many cases, bracket notation has the upper hand when it comes to accessing properties with dynamic names or names that include special characters, which dot notation can’t handle.

Dot Notation: Known for its simplicity and readability, dot notation is straightforward. It allows you to access the properties of a JSON object directly by their names. For instance, triggerBody().ID.name effortlessly fetches the name property from the trigger body of a Logic App that receives the previous input. This notation shines in its clarity, making code easier to read and understand at a glance.

Bracket Notation: Bracket notation, on the other hand, provides a level of flexibility unmatched by dot notation. It allows for the dynamic access of properties and the handling of property names that contain special characters or spaces.

Let’s take this JSON as an example:

{
  "user-info": {
    "first-name": "John",
    "last-name": "Doe"
  }
}

Here the bracket notation, (triggerBody()[‘user-info’][‘first-name’]) accesses properties that would be inaccessible with dot notation due to the presence of hyphens. This method is indispensable when dealing with dynamic property names or JSON objects with complex structures.

And, in case you were wondering, yes you can combine both methods together!

triggerBody().ID['name']

In conclusion, while Azure Logic Apps support both dot and bracket notations for accessing JSON properties, each notation has its strengths and best use cases.

Ultimately, the choice between dot and bracket notation depends on the specific requirements of your Logic App and the nature of the JSON data you’re working with. By understanding the strengths of each notation, you can leverage them effectively to build robust and adaptable Logic Apps tailored to your needs.

To lazy to read? We’ve got you covered! Check out our video version of this content!

Hope you find this helpful! If you enjoyed the content or found it useful and wish to support our efforts to create more, you can contribute towards purchasing a Star Wars Lego for my son!