Business Applications, Common Data Service, Flow, power automate, PowerApps, Uncategorized

Power Automate: PARSE JSON action with value or null

Power automate is endless fun!

Today we are going to explore a very “simple” issue which are 2 issues actually:

  1. how to define “allow null” in a schema to avoid annoying errors
  2. how to get away with the 1. without the painful consequences

Let’s start from the beginning!

PARSE JSON is a very useful action and we use it a lot in Power Automate.In my case I parse the response from a third-party API to populate a CDS entity. We can use a Generate from sample to auto-generate a schema.

PARSE JSON action

Which is totally awesome if you’ve got a sample which fully defines your schema. In my schema all values are optional. It doesn’t mean they will always be nulls but it’s bad enough to get a Power Automate error after the first run: “Invalid type. Expected String but got Null” So I understand that the generated schema has to be changed to allow nulls in the response.

We have to modify the schema manually.

There are 3 ways I know and I will list all 3 here below:

  1. Tell that Description can be null:
"LotReference": {
"type": [
"string",
"null"
]
}

2. Tell we have no idea what it is y removing a type mentioning completely:

"LotReference": {}

3. Using anyof in the definition:

 "LotReference": {
                "anyOf": [
                    {
                        "type": "string"
                    },
                    {
                        "type": "null"
                    }
                ]
            }

I will tell you something: Power Automate hates the first option! Yes, it stops complaining and even parses your response and throughs no errors but it “hides” the Dynamics content values related to the response.

The second option is OK. I will show you the screenshot, however there is another issue with it. It has to be converted via expressions or variables somehow after. If you response is as long as mine this is the last thing you want, really.

There is an option 2 and 1 on the screenshot below.

Options 1 and 2.

With the Dynamic content hidden for ContactReference (option 1) and available for OwnerReference(option 2).

Option 1- “type”:[“string”,”null] doesn’t generate a Dynamic content value.

No-type option 2 definition –

At last, the option 3 is there, in the Dynamic content, with the correct type!

Using anyOf in the schema to define nullable values
All available Dynamic content for PARSE JSON parsed user-friendly

If you want to learn more about PARSE JSON, read John’s blog post here: http://johnliu.net/blog/2018/6/a-thesis-on-the-parse-json-action-in-microsoft-flow

He uses a typeless option 2 in his example.

Happy parsing!

1 thought on “Power Automate: PARSE JSON action with value or null”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s