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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How to add a new prope...
Power Automate
Unanswered

How to add a new property to an object-type variable in Apply to Each?

(0) ShareShare
ReportReport
Posted on by 2

Hi

 

Inside an Apply to Each loop's Data-Compose action, if I add an expression using 'addProperty' to add a dynamically created key and value (both created using the loop item data) the object variable remains empty after the loop.

 

An example of the addProperty withing the Data-Compose action might be:

addProperty(variables('obj_var'), item()['key'], item()['value'])

On the loop completing the Data-Compose action Output contains an array of objects each containing a single property, rather than my hoped for single object containing the comma seperated list of key:value property pairs eg.

{ "name":"John", "age":30, "car":null } 

 

Is it possibe to add dynamically created key:values using addProperty to an object variable within a loop?

 

Thanks

Categories:
I have the same question (0)
  • v-yuazh-msft Profile Picture
    Microsoft Employee on at

    Hi @euanjfc,

     

    Could you please share a screenshot of the configuration of your flow?

    Could you please share more details about your scenes?

     

    I have made a test on my side and I couldn't find a way to add dynamically created key:values using addProperty to an object variable within a loop.

     

    Please share more details so we would try to provide a proper workaround for you.

     

    Best regards,

    Alice

  • euanjfc Profile Picture
    2 on at

    Hi

     

    Image of example flow  below. The addProperty expression is -

    addProperty(variables('obj_var'),item().item.key,item().item.value)

    Example flow

     

    and the final Compose output when run shows the empty object variable and the Compose output with an array of objects:

    {} [{"key1":"value1"},{"key2":"value2"}]

    Thanks

  • JeffCGrd Profile Picture
    14 on at

    The addProperty()  returns a new object.  It doesn't update the variable referenced by the first object.  You have to assign a second object variable the output from addProperty(variable('obj_var'), item().item.key, item().item.value).  Then assign that second variable back to 'obj_var'.  Flow won't let you assign directly to the same var.

    Here is a screen shot of my flow.  Configuration is the variable I want to get the new properties. (basically the same as your obj_var)

     

    2019-02-16 14_35_26-.png

  • LorenEdgell_HEI Profile Picture
    5 on at

    This is great, but what are the bottom two variable actions? Append to array doesnt work if you have defined an object...

    Can you please expand and maybe explain a little more? @JeffCGrd 

  • TomaszPoszytek Profile Picture
    170 Most Valuable Professional on at

    I am doing it the other way round. In a loop of items that I want to append to my object, I'm first using "union" function together with the "addProperty" and then "Set variable" so that ma unioned object is updated:2019-03-01_11h51_53.pngThe "union" operation looks as below:

    union(addProperty(variables('var_TempSubscInstanceName_dict'),item()?['Id'],item()?['Name']), variables('var_SubscInstanceName_dict'))

    So in the end it is joining together a new object created by "addProperty" and the existing object initiated as variable.

    Next I am simply updating (Set variable) my object with the result of the union operations.

     

    Regards,

    Tomasz

     

  • JeffCGrd Profile Picture
    14 on at

     

     

    Sorry for the late response.  My companies spam filter was catching these notifications.


    @LorenEdgell_HEI wrote:

    This is great, but what are the bottom two variable actions? Append to array doesnt work if you have defined an object...

    Can you please expand and maybe explain a little more? @JeffCGrd 


    They are just "Set Variable".  The variables are initialized as Object type.  AddProperty() returns an object and it is assigned to tmpObj.  Then because I want the final values in the configuration variable, I set configuration to the value in tmpObj.

  • BTbearings Profile Picture
    46 on at

    I know this was written 4 years ago, but I hope you might see this!  Do you actually need the union() command?  If addProperty() returns a new object with the new property added to it, doesn't that already achieve what you're trying to do?

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 784

#2
Valantis Profile Picture

Valantis 581

#3
Haque Profile Picture

Haque 545

Last 30 days Overall leaderboard