web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Checks if all properti...
Power Automate
Unanswered

Checks if all properties of an object exist and adds them if they are missing

(1) ShareShare
ReportReport
Posted on by 94

Sometimes it happens that we receive an object, but not all the properties have been defined, and the flow fails and throws an error because the property does not exist.

 

Since we know what properties we are going to use in the flow, what we have to do is create an object with all properties null and then perform a union of the two objects.

 

First I created a flow, in this case the trigger is manual, then I initialize two string variables:

 

The first is the string of the json object with all properties set to null

In this example an object is defined with three properties: year, month, day

{"year":null,"month":null,"day":null}

3tsolutions_0-1671701060299.png

 

The second is the string of the json object that has arrived, in this case simulated that one of the three properties is missing

{"year":2022,"month":12}

3tsolutions_1-1671701311357.png

 

Then I parse the two json strings to get the two objects

3tsolutions_4-1671701500587.png

 

 

3tsolutions_2-1671701423757.png

3tsolutions_3-1671701447084.png

 

Finally I define using Compose the union of the two objects with all the properties

union(body('Parse_JSON_Property'),body('Parse_JSON_String'))

3tsolutions_5-1671701538171.png

 

The result as you can see is the object complete with all properties and with all values, and for missing properties it will be assigned null values

3tsolutions_6-1671701623702.png

 

 

 

If you have nested objects, the child properties will not be automatically merged

In this example I added the time property with an object with three other child properties

{"year":null,"month":null,"day":null,"time":{"hour":null,"minute":null,"second":null}}

and I simulate that the object that arrived has no main properties and no child properties

3tsolutions_8-1671702922064.png

 

After doing the parse of the two string variables (see above)

I perform the union of the child properties separately from the main ones

union(body('Parse_JSON_Property')['time'],body('Parse_JSON_String')['time'])

3tsolutions_12-1671703691697.png

 

3tsolutions_9-1671703171175.png

 

As you can see merging two objects fails to merge the child properties

3tsolutions_11-1671703579961.png

 

So we need to set the property with children

setProperty(outputs('Union_JSON_Property_'), 'time', outputs('Union_JSON_Child_Property'))

3tsolutions_13-1671703760210.png

3tsolutions_14-1671703810286.png

 

 

Categories:
I have the same question (0)
  • grantjenkins Profile Picture
    11,063 Moderator on at

    @3tsolutions 

     

    It's normal for properties to be missing from your JSON if they don't have values.

     

    Can you specify in what circumstances you get errors?

     

    Note the two ways to retrieve a property - the second one won't fail if the property doesn't exist.

     

    //expects the property to exist and thows an error if it doesn't
    item()['name']
    
    //property may or may not exist - notice the ? used here for optional. If the name property doesn't exist it will just return null
    item()?['name']

     

     

  • 3tsolutions Profile Picture
    94 on at

    There are many cases where the absence of a property causes errors in the flow.
    From logical to mathematical operations, from operations with dates to more.

     

    Using the ? if the value of a property is not present it returns a null value,
    but in many cases you need a default value that is not null.

     

    For example if you perform a mathematical function

     

    add(body('JSON')?['missing_property'],1)

     

    3tsolutions_4-1671789021025.png

    or in a date and time function

     

    formatDateTime(body('JSON')?['missing_property'],'yyyy-MM-dd')

     

    3tsolutions_5-1671789196842.png

    or in a string function

     

    substring(body('JSON')?['missing_property'],5,2)

     

    Immagine 2022-12-23 110809.jpg

     

    When you try to use the value of a property the create a condition

    3tsolutions_6-1671789310034.png

     

    When creating or updating an item in a Sharepoint list, if using formulas

    Immagine 2022-12-23 110520.jpg

    Using my system I can define the correct default values ​​of object properties so that no errors occur during the flow.

     

  • grantjenkins Profile Picture
    11,063 Moderator on at

    @3tsolutions I was probably more referring to this post where you are defaulting all of the values to null which would still encounter all the errors you listed above. And using ?['property_name'] would already return null anyway.

     

    If you were talking about setting valid default values to get around these errors, then that would make sense.

  • AlexEncodian Profile Picture
    4,409 Moderator on at

    @3tsolutions 

     

    Thanks - I have the issues with an Azure DevOps query that is missing properties within objects when that property is empty and that causes a downstream Select action to fail.

     

    Your solution makes sense to me when you have 1 object that you want to add missing properties to. 

     

    If you have a JSON array with e.g. 100 objects, do you have to loop through each object and do the union operation? Is there are cleaner way of doing it?

  • powerdev-a Profile Picture
    25 on at

    Any updates on this? I have the same scenario on which I want to create csv file using Select Action but it fails due to a property that is missing.

  • powerdev-a Profile Picture
    25 on at

    when using Select action, using this approach "?['property_name']" is not helpful at all as this will throw error instead of null. It could be much better if it could return NULL instead and action error.

  • Chriddle Profile Picture
    8,441 Super User 2025 Season 2 on at

    Check the function coalesce() to use a default value in case of null.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 503 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 321 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard