Welcome again to another Logic Apps Best Practices, Tips, and Tricks. In my previous blog posts, I talked about 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 to process an Array or a single object JSON structure in the same manner
How to process an Array or a single object JSON structure in the same manner?
In one of my previous Best Practice, Tips, and Tricks, I spoke about How to validate if a JSON structure is an Array or a single object, which will be pretty valuable in many scenarios.
It is very common to come across APIs or Systems that, invoking the same operation, we will get two different but similar messages, for example in a specific condition, we may get one message containing a structure with a simple object, and in another situation, we will get not a single entity but instead an array of objects.
In these situations, what we usually see, but want to avoid, is having a condition to check if it is an Array or a single object, and then in each branch of the condition implementing the same business logic but in one branch configures to use an array, we usually see there a for each. and in the other branch, configured to use a single object. Similar to the picture below:
As I mentioned before, this is a situation we want to avoid because of any required change request that affects that specific logic. We will be forced to replicate twice, which may induce problems because if we are troubleshooting this type of scenario and find an issue in one of the branches, we may not remember that we need to apply the changes in both condition branches. If you are a new guy on the team, you may not know this “limitation”, and in the end, we have a huge probability of getting these two brunches out of sync.
So the main question is: How can we avoid this and singly process both scenarios?
where we may get a message structure containing the following:
- An array of Cars with a single element
{ "name": "Sandro", "cars":[ { "name": "Toyota" } ] }
- An array of Cars with multiple elements
{ "name": "Sandro", "cars":[ { "name": "Toyota" }, { "name": "Mercedes" } ] }
- An empty Cars array
{ "name": "Sandro", "cars":[] }
- Or a single-car structure
{ "name": "Sandro", "cars":{ "name": "Toyota" } }
For us to implement a single business logic that can process both situations, an array or a single object, we need to force a conversion f the car’s property into an array. To do that we need for example to do:
- On our business logic, we can add a Parse JSON action and apply the following configurations:
- In the Content property, add the following expression:
-
array(triggerBody()?[‘cars’])
-
- And for the Schema property, click in Use sample payload to generate schema and add the following JSON payload;
- In the Content property, add the following expression:
[
{
“name”: “Toyota”
},
{
“name”: “Mercedes”
}
]
This expression will force that in all situations, we will be using an array from now on.
- Then in our For each action, we will set the Select an output from the previous steps option to be the output of the Parse JSON action.
In this sample, we then, just for proof-of-concept, will be creating a string with a list of cars separated by |. In the end, we will be returning that list as plain text in the HTTP response. This will be the full business logic highlighting some parts that were not mentioned in details here:
Now, if we try our process using:
- A single-car structure, we will get the following successful response:
- An array of Cars with multiple elements, we will get a similar successful response:
- An array of Cars with a single element, again, we will get a similar successful response:
- An empty Cars array, we then receive an empty successful response:
Of course, in this POC we are assuming that the Cars element is mandatory and we are not worried about the layout aspect of the response (for example not to finish with |)
I hope you enjoy this developer tip and stay tuned for the following Logic App Best practices, Tips, and Tricks.