Hello everyone and welcome to my blog. In today’s blog, I will discuss on how you can check if an object property is null or missing in a Power Automate flow.
Through my blog I share interesting tips and discuss on latest releases of Microsoft.NET technologies, Dynamics 365 and Power Platform, SharePoint and Client scripting libraries. Please subscribe to my blog to stay updated.
Let’s get started. For this blog, I will use the below JSON object.
[
{
"FirstName": "Debajit",
"LastName": "Dutta",
"EmpCode": "1111",
"Department": {
"Code": "222",
"Name": "IT"
}
},
{
"FirstName": "Jason",
"LastName": "Williams",
"EmpCode": "1112"
}
]
It’s a simple JSON array with couple of elements. Observe carefully that the second element in the array does not have the Department property.
My requirement here is to iterate through this array and check if the Department for an item is missing.
To test it, I created an instant flow. I used an Array variable to store the JSON array
The next step is to iterate through the array elements and check if the Department property is missing or is null.
For that, I used the empty function of Power Automate. Precisely I used the below expression inside IF condition.
empty(items('Apply_to_each')['Department'])
However when I ran the flow, I received the below error.
‘The template language expression ’empty(items(‘Apply_to_each’)[‘Department’])’ cannot be evaluated because property ‘Department’ doesn’t exist
Quite obvious the property does not exist but as it seems, the empty function is not able to handle missing properties. But how do I do it?
Well, we are going to use the same expression with a little twist. And below is the modified expression.
empty(items('Apply_to_each')?['Department'])
Did you notice any change? If you observe carefully, you notice the use of “?” operator, right after items(‘Apply_to_each’).
Always use the ? operator where you are not sure of the outcome of an expression. The use of ? operator ensures that even if the property value is null or is missing, it won’t throw an error during evaluation.
Now when I run the flow, it works fine.
Hope this helped. For similar interesting topics on Microsoft.NET and Power platform, you can subscribe to my blog.
Debajit Dutta
Business Solutions MVP
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.