Try for free Book a demo

Logic App Best Practices, Tips, and Tricks: #32 Specifying JSON Schema required elements

Microsoft Azure

4 Mins Read

specify json schema element in logic app

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 post, we explained how to specify nullable elements inside our JSON. Today we will continue on the same topic, being JSON Schemas. This time I will speak about another Best practice, Tips, and Tricks that you must consider while designing your business processes (Logic Apps): Specifying JSON Schema required elements.

specify json schema element in logic app

Specifying JSON Schema required elements

A JSON Schema does not specify any element to be required unless it is specifically defined. That means that, by default, when you generate a JSON Schema inside Logic Apps, all elements are optional. In other words, they can be absent from the JSON payload.

Taking the following valid payload as a proof-of-concept:

{
   "name": "Sandro Pereira",
   "age":44,
   "gender": "Masculine"
}

By default, this is also a valid payload:

{
   "name": "Sandro Pereira",
   "Gender": "Masculine"
}

If you wish to configure required elements/properties inside the message, you need to specify them explicitly inside the JSON Schema.

In this proof-of-concept, the goal is to define the age element/property required inside our Logic App. To accomplish that, we will create a Logic App that receives a message over HTTP and then return a response with the value of the JSON Schema validation.

To specify a JSON schema in our Request > When an HTTP request is received trigger is quite simple, and the same happens with many triggers. This trigger contains a Request Body JSON Schema property in which we can add our JSON Schema that describes the properties and values in the incoming request body.

Adding JSON Schema Request Body property

The default value is an empty JSON Schema. If we don’t have a JSON Schema, we can generate a JSON schema based on the expected payload (data) by using external tools like https://jsonschema.net/ or directly in the Logic App designer, in the Request trigger, by selecting the Use sample payload to generate schema option:

Using sample payload to generate schema

On the Enter or paste a sample JSON payload window, enter the sample payload, and select Done.

specify json schema element in logic app

Of course, this JSON Schema generation can be used in many actions inside our Logic App workflow, not only in the trigger.

Now as you can see, when we first generate the schema, by default, there are no required properties:

{
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        },
        "age": {
            "type": "integer"
        },
        "gender": {
            "type": "string"
        }
    }
}

That means that all of them are optional.

If we  try using Postman with a valid message, we will get a 200 OK:

JSON schema with Postman integration

If we send a request without the age, we will also get a 200 OK:

json schema element in logic app

In order to specify the mandatory properties or elements, we need to use the required keyword, where you can specify a list of strings that need to be present as key names in the list of key:value pairs that appear in a JSON document. Each of these strings must be unique.

In our case, in order for the age element to be required, the schema should be:

 {
   "type": "object",
   "required": ["age"],
   "properties": {
       "name": {
           "type": "string"
       },
       "age": {
           "type": "integer"
       },
       "gender": {
           "type": "string"
       }
   }
}

Specifying age in JSON schema

Now, if we test our Logic App once again without sending the age, we will get a 400 Bad Request:

Testing Logic App without sending age

Note: the required keyword is used at any type of object level. For example, let’s say that we want to specify a football team:

{
   "club_name":"FC Porto",
   "country": "Portugal",
   "league":{
      "name": "Liga Portugal Bwin",
      "type": "Professional"
   }
}

They should have a club_name and league. Furthermore, the league should consist of a name. We achieve this using the following schema:

{
    "properties": {
        "club_name": {
            "type": "string"
        },
        "country": {
            "type": "string"
        },
        "league": {
            "properties": {
                "name": {
                    "type": "string"
                },
                "type": {
                    "type": "string"
                }
            },
            "required": [
                "name"
            ],
            "type": "object"
        }
    },
    "required": [
        "club_name",
        "league"
    ],
    "type": "object"
}

I hope you enjoy this developer tip and stay tuned for the following Logic App Best practices, Tips, and Tricks.

Related reading

Related reading

This article was published on Jun 19, 2023.

Related Articles