How to solve the JSON Invalid type error in Power Automate?

I get this question quite frequently in multiple variations, but the problem is always the same. You create a schema using a JSON that you get from somewhere, but when you run your Flow, you get an “Invalid type. Expected X but got Null”. The problem has a simple solution (you can check it below), but I’ll first show you why it happens to better frame the solution.

The problem

The issue is in the schema required to run the Parse JSON action requires to run. When you use the “Generate from Sample”, Power Automate will try its best to build the schema that describes the data received.

You don’t need to know what a schema is, but if you’re curious, here’s an article explaining it in detail.

The schema describes the type of data that the JSON has. Think of it as a translator between JSON and “Power Automate language” so you can then pick the fields from the “Dynamic content” in other actions.

For example, let’s say that we have the following JSON:

{
	"name": "Manuel Gomes",
	"age": 41,
	"address":""
}

Power Automate will generate the following schema:

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

If we insert a “Compose action”, for example, we can now find the values in the “Dynamic Content” tab:

But what about if I was shy about my age and didn’t provide it so that the service would return something like this:

 {
	"name": "Manuel Gomes",
	"age": null,
	"address":""
}

We will get a “ValidationFailed. The schema validation failed.”

Power Automate provides a bit more information below with the error message.

“Invalid type. Expected Integer but got Null.”

If the field were a string, then you would get the following:

“Invalid type. Expected String but got Null.”

So now that we have the error simulated, let’s see how to fix it.

The solution

The solution is to tell Power Automate that it should be able to receive both integers and null values. This is because a “null” value differs entirely from an integer or a string. A “null” value is not the same as “empty” since an “empty” string is a string nevertheless. A “null” field indicates that the field doesn’t have “anything” inside it. And since Power Automate doesn’t know if “anything” is the same as a string or an integer, it defines it as a distinct type.

So let’s add the “null” as a possible value, but how do we know what field has issues? To find it is easy. Just look at the fields below the error message:

In this case, it’s indicating that the “property” called “age” has the problem.

Now that we know where to fix it, let’s look again at the schema:

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

Currently, the “type” for the “age” is only an integer, but we need to say Power Automate to allow “null” as well. Since we want “multiple” types, we need to use an array of potential types of values, like:

["integer","null"]

Now we replace the type from only one value with a range of values.

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

And the Flow should run without issues. If you get this error again, please check the error message above and see the field’s name. You may have multiple fields that need to be changed, so repeat the steps until your Flow runs without issues.

Final thoughts

The solution to this issue is relatively easy once you understand where it comes from. Please be careful editing more complex schemas. You can always generate a new one and start over, but take your time and check if you’re replacing the field with problems.

Have a suggestion of your own or disagree with something I said? Leave a comment or interact on Twitter and check out other Power Automate-related articles here or if you want to check other solved problems, click here.

Photo by Michael Dziedzic on Unsplash

 

Manuel Gomes

I'm a previous Project Manager, and Developer now focused on delivering quality articles and projects here on the site. I've worked in the past for companies like Bayer, Sybase (now SAP), and Pestana Hotel Group and using that knowledge to help you automate your daily tasks

View all posts by Manuel Gomes →

Leave a Reply

Your email address will not be published. Required fields are marked *