cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
3tsolutions
Advocate II
Advocate II

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

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

 

 

3 REPLIES 3
grantjenkins
Super User
Super User

@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']

 

 


----------------------------------------------------------------------
If I've answered your question, please mark the post as Solved.
If you like my response, please consider giving it a Thumbs Up.

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.

 

@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.


----------------------------------------------------------------------
If I've answered your question, please mark the post as Solved.
If you like my response, please consider giving it a Thumbs Up.

Helpful resources

Announcements

Power Platform Connections - Episode 7 | March 30, 2023

Episode Seven of Power Platform Connections sees David Warner and Hugo Bernier talk to Microsoft MVP Dian Taylor, alongside the latest news, product reviews, and community blogs.     Use the hashtag #PowerPlatformConnects on social media for a chance to have your work featured on the show!      Show schedule in this episode:    0:00 Cold Open 00:30 Show Intro 01:02 Dian Taylor Interview 18:03 Blogs & Articles 26:55 Outro & Bloopers    Check out the blogs and articles featured in this week’s episode:    https://francomusso.com/create-a-drag-and-drop-experience-to-upload-case-attachments @crmbizcoach https://www.youtube.com/watch?v=G3522H834Ro​/  @pranavkhuranauk https://github.com/pnp/powerapps-designtoolkit/tree/main/materialdesign%20components @MMe2K​ https://2die4it.com/2023/03/27/populate-a-dynamic-microsoft-word-template-in-power-automate-flow/ @StefanS365 https://d365goddess.com/viva-sales-administrator-settings/ @D365Goddess https://marketplace.visualstudio.com/items?itemName=megel.mme2k-powerapps-helper#Visualize_Dataverse_Environments @MMe2K    Action requested:  Feel free to provide feedback on how we can make our community more inclusive and diverse.    This episode premiered live on our YouTube at 12pm PST on Thursday 30th March 2023.    Video series available at Power Platform Community YouTube channel.    Upcoming events:  Business Applications Launch – April 4th – Free and Virtual! M365 Conference - May 1-5th - Las Vegas Power Apps Developers Summit – May 19-20th - London European Power Platform conference – Jun. 20-22nd - Dublin Microsoft Power Platform Conference – Oct. 3-5th - Las Vegas    Join our Communities:  Power Apps Community Power Automate Community Power Virtual Agents Community Power Pages Community    If you’d like to hear from a specific community member in an upcoming recording and/or have specific questions for the Power Platform Connections team, please let us know. We will do our best to address all your requests or questions.       

Announcing | Super Users - 2023 Season 1

Super Users – 2023 Season 1    We are excited to kick off the Power Users Super User Program for 2023 - Season 1.  The Power Platform Super Users have done an amazing job in keeping the Power Platform communities helpful, accurate and responsive. We would like to send these amazing folks a big THANK YOU for their efforts.      Super User Season 1 | Contributions July 1, 2022 – December 31, 2022  Super User Season 2 | Contributions January 1, 2023 – June 30, 2023    Curious what a Super User is? Super Users are especially active community members who are eager to help others with their community questions. There are 2 Super User seasons in a year, and we monitor the community for new potential Super Users at the end of each season. Super Users are recognized in the community with both a rank name and icon next to their username, and a seasonal badge on their profile.  Power Apps  Power Automate  Power Virtual Agents  Power Pages  Pstork1*  Pstork1*  Pstork1*  OliverRodrigues  BCBuizer  Expiscornovus*  Expiscornovus*  ragavanrajan  AhmedSalih  grantjenkins  renatoromao    Mira_Ghaly*  Mira_Ghaly*      Sundeep_Malik*  Sundeep_Malik*      SudeepGhatakNZ*  SudeepGhatakNZ*      StretchFredrik*  StretchFredrik*      365-Assist*  365-Assist*      cha_cha  ekarim2020      timl  Hardesh15      iAm_ManCat  annajhaveri      SebS  Rhiassuring      LaurensM  abm      TheRobRush  Ankesh_49      WiZey  lbendlin      Nogueira1306  Kaif_Siddique      victorcp  RobElliott      dpoggemann  srduval      SBax  CFernandes      Roverandom  schwibach      Akser  CraigStewart      PowerRanger  MichaelAnnis      subsguts  David_MA      EricRegnier  edgonzales      zmansuri  GeorgiosG      ChrisPiasecki  ryule      AmDev  fchopo      phipps0218  tom_riha      theapurva  takolota     Akash17  momlo     BCLS776  Shuvam-rpa     rampprakash  ScottShearer     Rusk  ChristianAbata     cchannon  Koen5     a33ik  Heartholme     AaronKnox  okeks      Matren   David_MA     Alex_10        Jeff_Thorpe        poweractivate        Ramole        DianaBirkelbach        DavidZoon        AJ_Z        PriyankaGeethik        BrianS        StalinPonnusamy        HamidBee        CNT        Anonymous_Hippo        Anchov        KeithAtherton        alaabitar        Tolu_Victor        KRider        sperry1625        IPC_ahaas      zuurg    rubin_boer   cwebb365   Dorrinda   G1124   Gabibalaban   Manan-Malhotra   jcfDaniel   WarrenBelz   Waegemma   drrickryp   GuidoPreite    If an * is at the end of a user's name this means they are a Multi Super User, in more than one community. Please note this is not the final list, as we are pending a few acceptances.  Once they are received the list will be updated. 

Register now for the Business Applications Launch Event | Tuesday, April 4, 2023

Join us for an in-depth look into the latest updates across Microsoft Dynamics 365 and Microsoft Power Platform that are helping businesses overcome their biggest challenges today.   Find out about new features, capabilities, and best practices for connecting data to deliver exceptional customer experiences, collaborating, and creating using AI-powered capabilities, driving productivity with automation—and building towards future growth with today’s leading technology.   Microsoft leaders and experts will guide you through the full 2023 release wave 1 and how these advancements will help you: Expand visibility, reduce time, and enhance creativity in your departments and teams with unified, AI-powered capabilities.Empower your employees to focus on revenue-generating tasks while automating repetitive tasks.Connect people, data, and processes across your organization with modern collaboration tools.Innovate without limits using the latest in low-code development, including new GPT-powered capabilities.    Click Here to Register Today!    

Check out the new Power Platform Communities Front Door Experience!

We are excited to share the ‘Power Platform Communities Front Door’ experience with you!   Front Door brings together content from all the Power Platform communities into a single place for our community members, customers and low-code, no-code enthusiasts to learn, share and engage with peers, advocates, community program managers and our product team members. There are a host of features and new capabilities now available on Power Platform Communities Front Door to make content more discoverable for all power product community users which includes ForumsUser GroupsEventsCommunity highlightsCommunity by numbersLinks to all communities Users can see top discussions from across all the Power Platform communities and easily navigate to the latest or trending posts for further interaction. Additionally, they can filter to individual products as well.   Users can filter and browse the user group events from all power platform products with feature parity to existing community user group experience and added filtering capabilities.     Users can now explore user groups on the Power Platform Front Door landing page with capability to view all products in Power Platform.      Explore Power Platform Communities Front Door today. Visit Power Platform Community Front door to easily navigate to the different product communities, view a roll up of user groups, events and forums.

Microsoft Power Platform Conference | Registration Open | Oct. 3-5 2023

We are so excited to see you for the Microsoft Power Platform Conference in Las Vegas October 3-5 2023! But first, let's take a look back at some fun moments and the best community in tech from MPPC 2022 in Orlando, Florida.   Featuring guest speakers such as Charles Lamanna, Heather Cook, Julie Strauss, Nirav Shah, Ryan Cunningham, Sangya Singh, Stephen Siciliano, Hugo Bernier and many more.   Register today: https://www.powerplatformconf.com/   

Users online (1,879)