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. Check out these Logic App tips and tricks!
In the last two posts, we addressed validating whether a string or an array was null or empty. Today we will continue on the same topic, validations, and I will speak about another good Best practice, Tips, and Tricks that you must consider while designing your business processes (Logic Apps): How to validate if a JSON structure is an Array or a single object.
How to validate if a JSON structure is an Array or a single object
Don’t get me wrong, I like JSON, but most of the systems and developers think that because JSON is so open, it doesn’t have any rules, which makes it challenging to integrate with these end systems or REST services because, depending on the scenario, you may get one type of response and in another scenario a similar but different response type. An excellent example of this is a list or an array.
JSON Object Structure is surrounded by curly braces {}, and it can contain:
- zero
{ } //Empty JSON object
- one
{ "name": "Toyota" }
- or more key-value pairs, also called properties.
{ "name": "Toyota", "year": 2021 }
On the other hand, a JSON Array Structure is surrounded by square brackets [ ], and it can contain the following:
- Zero elements
{ "name": "Sandro", "cars":[] }
- One element
{ "name": "Sandro", "cars":[ { "name": "Toyota" } ] }
- Or more than one element separated by a comma.
{ "name": "Sandro", "cars":[ { "name": "Toyota" }, { "name": "Mercedes" } ] }
To simplify this scenario, assuming that the structure ‘cars‘ is mandatory, we can receive one element or many elements. Ideally, the ‘cars‘ structure being an array, the expected JSON structure should be what was described above. However, many systems don’t respect that “rule”. Instead, if the response only has a single car, then we will receive the following response type:
{ "name": "Sandro", "cars":{ "name": "Toyota" } }
And if the response has multiple cars, then we get an array:
{ "name": "Sandro", "cars":[ { "name": "Toyota" }, { "name": "Mercedes" } ] }
Of course, these two types of responses must be dealt with differently. This is quite simple to accomplish in traditional languages like C#. So, if you want to know if an object is an array. One way to do it is:
if (obj.GetType().IsArray) { ... }
Another way you can do it is
if (obj is Array) { // ... }
But can we achieve the same inside Logic Apps in an easy way?
And the answer is: yes, we can! It will seem a little weird how we can validate, but it will do the trick!
Let’s assume we will send the JSON sample described above in this small proof-of-concept to our Logic App.
The simple way to achieve this validation inside Logic Apps is by using the following condition statement:
string(triggerBody()?['cars']) starts with [
As you can see in the picture below:
Of course, you may find different ways to achieve the same.
Now if we try to test our solution and send an array to our Logic App, we will get the response saying, “Is an array“:
On the other hand, if we send an object, we will receive the response saying, “Is an object“:
I hope you enjoy this developer tip and stay tuned for the following Logic App Best practices, Tips, and Tricks.