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
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
Hi
Image of example flow below. The addProperty expression is -
addProperty(variables('obj_var'),item().item.key,item().item.value)
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
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)
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
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:The "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
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.
User | Count |
---|---|
95 | |
45 | |
21 | |
18 | |
17 |
User | Count |
---|---|
142 | |
50 | |
43 | |
40 | |
31 |