Integer 123 is not a multiple of 10

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

Use 1 Parse JSON action to validate data 1

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.

Use 1 Parse JSON action to validate data 2

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.

Parse JSON action validating Integer 123 is not a multiple of 10

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.

Use 1 Parse JSON action to validate data 3

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.

One thought on “Use 1 Parse JSON action to validate data

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: