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 use while working with Azure Logic Apps. Check out these Logic App tips and tricks!
In the last three blog posts, we explained how to validate null inside Logic App and specifying json schema elements/properties and perform JSON Schema restrictions in Logic Apps. Today and to finish this topic, at least for now, I will speak about another best practice, Tips and Tricks that you must consider while designing your business processes (Logic Apps): Validating JSON messages against a schema in Logic Apps.
Validating JSON messages against a schema in Logic Apps
Sometimes we want to send a message to a Logic App, but before we process it, we want or we need to validate the same message against a JSON Schema to make sure that the required fields are present in the message, the type of the fields are correct, and so on.
While working with XML messages, this task is quite obvious and easy. We have an XML Validation action to accomplish that:
Of course, if we work with Logic App Consumption, we need to use an Integration Account to accomplish this task. If we use Logic App Standard, we can use the built-in capabilities without needing an Integration Account. However, we can not apply the same techniques when dealing with JSON messages. There isn’t a JSON validation action inside Logic Apps, so we cannot apply the same approach as XML message validation.
However, we can always achieve this task easily using an Azure Function. The goal here is to understand if there are possible ways to achieve this using the default capabilities inside Logic Apps, and the response is: yes, we can!
At least there are two possible ways:
- Using the Request > When a HTTP request is received trigger.
- or using the Parse JSON action.
There may be other triggers that allow you to specify a JSON schema, but I don’t remember any other trigger with these same capabilities.
Request > When a HTTP request is received trigger
To run your logic app workflow after receiving an HTTPS request from another service, you can start your workflow with the Request > When a HTTP request is received built-in trigger. One of the great things about this trigger is that it does not only allow you to specify a Request Body JSON Schema:
- The default value is an empty JSON Schema.
- If we don’t have a JSON Schema, we can click on the Use sample payload to generate schema option:
- On the Enter or paste a sample JSON payload window, enter the sample payload, and select Done.
- In order to automatically generate the JSON schema based on the sample message you provide.
But it also allows us to specify if we want to validate the inbound message or not by turning the Schema Validation setting on or off. To accomplish that, we need to:
- On the Request > When a HTTP request is received trigger, click on … (3 dots) and select the Settings option.
- On the Settings for ‘When a HTTP request is received’ window, enable the Schema Validation option and then click Done.
Now, every time we send a JSON message to our Logic App, that message will be validated against the JSON schema, and if it’s invalid, we will end up receiving an HTTP 400 response.
Parse JSON action
The other option to perform the same task, which is very useful when you receive an HTTP response or if you use other triggers in our Logic App, is to use the Parse JSON action.
However, we also can specify some restrictions in our JSON Schema, like the required fields in the Parse JSON action.
I didn’t mention it earlier, but the Parse JSON action has the same capabilities in terms of specifying and generating a JSON schema as the Request > When a HTTP request is received trigger.
The most significant difference is that the Parse JSON action does not have a property Schema Validation option that you can turn on and off. It will always validate the JSON Schema.
I hope you enjoy this developer tip and stay tuned for the following Logic App Best practices, Tips, and Tricks.