“I’ve got an array where each property is a separate object, how can I turn it into a single object using Power Automate flow?”
If you stay only in the Microsoft 365 environment, you probably won’t encounter this problem. Every array is either a simple array or an array with objects. But once you start processing data from other systems it might not be as clear. Instead of a single object you might receive an array of objects with each object representing one property. A format that can make your life quite complicated.
How do you turn it into a single object that’s easy to work with?
Convert the array into a string
I believe that the easiest way is to “brute force” the conversion.
To turn it into a single object you must remove the },{ between the objects and replace them with a comma. But to use the replace(…) expression you’ll need a string, not an array.
string(outputs('Compose'))
Replace the },{ characters
Once you have a string, you can replace some parts of it. In this situation you should replace the },{ with a single comma.
replace(string(outputs('Compose')),'},{',',')
Turn it back into an array
The string now resembles the object, but it’s still a string. You can’t select a value from a string, nor can you navigate in an array-like looking string. You must turn it back into an array with an object using the json(…) expression.
json(replace(string(outputs('Compose')),'},{',','))
Remove the array
The last step is to remove the [ ] brackets representing an array as you don’t need it anymore since it’s a single object. Use the first(…) expression to take only the first (and only) object.
first(json(replace(string(outputs('Compose')),'},{',',')))
And that’s it, you just turned an array of objects into a single object in your Power Automate flow.
Summary
You don’t need a fancy solution for everything when using Power Automate. Sometimes it’s fine to just adjust the data and force it into the desired format. All you have to do is follow your thought process – how would you do that step by step? Turn it into a combination of expressions and move on.
Hi, thanks for your shares.
What about if I d like to use “append to array variable” action, in a context of apply to each (preventing me from using set variable action in concurrency) ?
The append to array value can’t have the brackets…
Thanks
Hello Joseph,
I’m not sure that I understand the situation, but one of the steps in the blog post is removing the [ and ] brackets using the first(…) expression.
Hi, Tom!
I’ve used this solution forever since i first found it, so i’m very grateful to you for posting.
I must have copy/pasted this handy and quick one-step Compose in a dozen Flows…
I’ve always been a bit wary about how string-manipulation hacks point to limitations in a language. Such as it is with Power Automate/Workflow Definition Language : )
And then a wonderful little sub-culture of talented individuals emerges around much-needed clever workarounds to these limitations, which i enjoy, so that part’s fun!
And i have an update in response so i’m really glad i found you again in this original post!
Anyway, in this particular case, it sure beats the *very* slow Apply-to-each, so, again, thank you for sharing your solution.
But one limitation of this particular solution had been bugging me: this works great for an array of objects with flat k-v pairs. But if you have any array nested under the ‘v’, the ‘},{‘ -> ‘,’ string substitution collapses those too.
So i found out a way to do a fast one-stepper with xml + xpath which preserves the inner-structure of any values in the mini-objects.
Here’s another way to faithfully merge an arbitrarily-sized array of JSON objects into a single object:
json(xml(concat(”, join(
xpath(
xml(json(concat(‘{ “root”: { “arr”: ‘,
outputs(‘ObjArray’),
‘}}’))),
‘/root/arr/*’
),
”), ”))).root
I do hope my formatting of the WtfDL above stays somewhat preserved.
Basically:
1) xmlify the JSON with prepended /root/arr element,
2) then select all the array’s sub-elements with xpath /root/arr/*
3) join the results with empty delim, wrap in another , convert from xml back to JSON, and select the .root!
I hope this may help someone else facing the Apply-to-each grind… : )
(and I hope this works in more than just my toy dataset…!).
Hello Gerald,
thank you for sharing the solution! I’ll have to try it and add all that’s necessary, the comments unfortunately remove some important characters when you enter a piece of code. :/
the formatting pulled out my ” from the final line, before the triple-parens
: )
aaaugh, the formatting pulled out my:
open-broket ‘root’ close-broket
from the outermost concat, at the top,
and at the bottom with forward slash to close the tag