Categories: Microsoft 365

Use 1 Parse JSON action to validate data

Have you used the Parse JSON Action to generate Dynamic Content? Did you know that you can use it to validate json data?

Introduction to Parse JSON

The Parse JSON is often used to generate the dynamic content in flow so that it is easier to select data rather than query the raw json

In the above example I could use the Compose output given to me by the dynamic content however it is easier to use the output generated by the Parse JSON action. If you didn’t use the parse JSON you might have to query json using something like this:

outputs('Compose')?['text']

Selecting the text dynamic content is simply easier using the Parse JSON action.

Configure the Parse JSON action

To configure JSON you specify where the Parse JSON action gets the data from in the Content field of the Parse JSON action. Then click on the generate from sample button and your Schema is generated.

Most of the time you will be done now. But you can do more!

JSON schemas

Before jumping into the advanced configurations of the Parse JSON action, I will have a look at json schemas.

In the above examples you can see that a json scheme is written using json.

In short you can specify data structures using the following types:

  • object
  • array
  • integer
  • string

But there are quite a few more when you look at the json schema site referenced above.

There are also some other things that you can do with data. One of these things is data validations. The operation multipleOf is one of those available. Other options are:

  • minimum
  • maximum
  • exclusiveMaximum
  • exclusiveMinimum
  • Pattern

For pattern you could for example specify the following regular expression:

"pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$"

In this post I will use the simple multipleOf operation. In one of my follow up posts I will have a look at the more complicated regular expression option.

Validate Data with the Parse JSON action

Now we get to the magic of this post!

My action generated the following schema for me. In here you can recognize the structure as mentioned above. This is all following standard as expected. This is also where you can noticed that dates are processed as text or string values rather than a specific date type.

{
    "type": "object",
    "properties": {
        "value": {
            "type": "integer"
        },
        "text": {
            "type": "string"
        },
        "date": {
            "type": "string"
        }
    }
}

Now I want to validate my data. I wonder what would happen when I add the following line to my definition of the value field?

"multipleOf": 10

Resulting in the following json code. Note that you have to add the extra comma after the previous line.

{
    "type": "object",
    "properties": {
        "value": {
            "type": "integer",
            "multipleOf": 10
        },
        "text": {
            "type": "string"
        },
        "date": {
            "type": "string"
        }
    }
}

When I now run my flow, I will get a failed action to return the following message as my json data is validated.

ValidationFailed: The schema validation failed.

This is exactly what I want. I get a failure when my data doesn’t match the required data specifications. In my case 123 is not a multiple of 10 and therefore I will see the failure.

When you feed the Parse JSON action with the number 120 you will find that the above action is successful. You might need to add a bit of error handling in your flow of course as validation might not immediately need to fail a flow.

Regular Expressions

This is the bit that I was really looking forward to. But unfortunately the pattern/regular expressions part of the json schema hasn’t been implemented.

The next steps

Now you could connect this idea with the Try-Catch pattern and you have a full data validation pattern available in your flows.

Just imagine how many if steps you could remove from your existing flows! There must be so many possibilities to use this pattern.

For more possible data validation work I recommend to go through some validation examples. These examples include so many practical problems. Just to give some ideas of the examples:

  • Validate telephone numbers
  • Ensure a billing address is given when a credit card number has been supplied

Do notice however that not all options available within the json schema are available within the JSON Parse action.

Share
Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

View Comments

Recent Posts

Calculate the Sum for a SharePoint column in Power Automate

Last week Shane Young asked me about calculating the Sum for a SharePoint column in…

4 hours ago

Object must implement IConvertible in Power Apps

In Power Apps when you do a Patch to create a new item or to…

4 days ago

Get started with adaptive cards in Power Automate

Getting started with adaptive cards can be difficult. In this post i have written some…

1 week ago

Create PDF documents from data in Power Automate

In this post I will look at how to create PDF documents from data. Use…

2 weeks ago

Unnest nested arrays in Power Automate

We all know this problem, you have a nested array in Power Automate but how…

3 weeks ago

Advanced settings not loading in Power Platform

Yesterday one of my clients showed me an issues where the Advanced settings didn't load.…

4 weeks ago
%%footer%%