Try for free Book a demo

Logic App Best Practices, Tips, and Tricks: #31 Specifying JSON Schema elements nullable

Microsoft Azure

3 Mins Read

json schema null inside 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 have while working with Azure Logic Apps. Check out these Logic App tips and tricks!

Today I will speak about another Best practice, Tips, and Tricks that you must consider while designing your business processes (Logic Apps): Specifying JSON Schema elements nullable.

specifying json elements nullable

Specifying JSON Schema elements nullable

A JSON Schema does not allow null as a value unless specifically allowed. That means that, by default, when you generate a JSON Schema inside a Logic App, a null value would not be valid. If you wish to allow null values for those data types, you need to allow null as a type for each element explicitly.

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

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

The goal of this proof-of-concept is to allow null values on the gender element inside our Logic App. To accomplish that, we will create a Logic App that receives a message over HTTP and then returns a response with the value of the gender element.

To specify a JSON schema in our Request > When a 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.

Add JSON schema to Request Body JSON Schema 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 jsonschema.net or directly in the Logic App designer, in the Request trigger, by selecting Use sample payload to generate schema option:

Use sample payload to generate schema

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

Enter or paste a sample JSON payload inside 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, the gender element is a string type:

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

That means that it doesn’t accept null.

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

json schema null inside logic app

But if we send a null value, we will get a 400 Bad Request:

json schema null inside logic app

It’s important to remember that in JSON, null isn’t equivalent to something being absent!

{ "type": "null" }

This means that if the value of that element is:

  • null – that is valid!
  • false – that is invalid.
  • 0 (zero or any number) – that is invalid.
  • ” “ – that is invalid.
  • Absent – that is invalid.

So, once again, by default, when we generate our JSON Schemas inside Logic Apps, it does not allow any of these fields in the schema to be nullable. If you try to parse a record set with a null value, an error will be thrown. To allow nulls to be set, edit the schema type for each element you want and convert the type into an array of types, including the null type [“…”, “null”].

In our case, in order for the gender element to be nullable, the schema should be:

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

json schema null inside logic app

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

Related reading

This article was published on Jun 14, 2023.

Related Articles