A Thesis on the Parse JSON action in Microsoft Flow
Parse JSON can be both intelligent and dumb. It can help you a lot and it can hinder you just as much.
This blog post, we go deep and study the behaviour of the Parse JSON action, it's various problems and solutions.
Here we tackle the question - How do you use Parse JSON, actually?
Plan
- Getting Started with Parse JSON
- Problem - way too many properties
- Problem - the array - item object and auto-wrapping
- Problem - duplicate properties names?
- Problem - null value properties
- Problem - missing properties
- How to quickly fix the schema
- Result is Parse JSON will make the Dynamic Content Panel actually intelligent
Getting Started with Parse JSON
In general, the reason we use Parse JSON is because Flow doesn't know the format of the data we are receiving in our actions. So we always start by running the actual method once - even if the Flow has only been partially completed. We want to get a sample of the data we'll be seeing.
Look at all these properties in the Dynamic Content Panel. Looks amazing. Our problems are about to happen. Don't worry we'll fix all of them.
Problem 1 - way too many properties
The schema Parse JSON generates is very very verbose. So there are a lot of properties that we probably just don't care.
So copy the generated JSON Schema from the Parse JSON action, and use a text editor to have a look at it. Here I'm using VS Code - switch the editor mode to JSON.
We see it has generated type information for various _links properties, or in this example, the halfTime results or odds of a football game. We don't need this, so we can delete it from the schema.
Problem 2 - the array - item object and auto apply-each wrapping
Notice under the fixtures (array) we have an items property with each object. This appears under Dynamic Content Panel as "fixtures - Item"
Becareful when clicking on this property, it will immediately unfold with an For-Each block.
It depends whether this is actually what we want - typically, when we aren't in a For-Each block, this is helpful. But if we are already inside a For-Each block this may create a second nested for-each block and be quite unnecessary and confusing.
Problem 3 - duplicate properties names?
Hey why do I see duplicate names? How can we tell which one is which?
The simplest way to ease this problem is to delete the properties in the schema that you don't want (same as problem 1). This will reduce the number of duplicate properties that appears. But sometimes, we still get some duplicate names - often I see the top level object has a 'name' and the child object has another 'name'
In this case, I check with the hover over to make sure I've selected the right choice.
Problem 4 - null value properties
(This problem category also applies to all type mismatch errors)
In general, Parse JSON generates the schema based on the first object it sees from the sample. This is not always correct - since sometimes the properties may be null in the later objects. For example in the football games data - earlier records has scores as integers, but later records haven't been played yet so scores are null.
This error usually fails the Flow. Another mutation of this problem is when the number is a decimal, but the schema thinks it's integer.
The easiest way to fix nullable-values is to delete the type information for this property.
Using no type information is better than changing to something like type: "object" because the Dynamic Content Panel will always show these properties.
If goalsAwayTeam was set to object, then it wouldn't be available here.
Problem 5 - missing properties
Sometimes, a later object in the array doesn't have a property at all.
The fix is simple - remove the "y" from "required"
But sometimes this is half the problem - because the expression used after this may expect the property to exist and always available.
Result
Parse JSON will make the Dynamic Content Panel actually quite intelligent. But it requires some clean up maintenance on the JSON schema.
It is up to each scenario to decide if there's value to fix the JSON Schema, or to skip using Parse JSON and just use item()?['property-name'] expressions directly.