Let's say I have two arrays of objects:
[
{
"key": "a",
"value": [1,2,3]
},
{
"key": "b",
"value": [1,2,3]
},
{
"key": "c",
"value": [1,2,3]
}
]
and
[
{
"key": "b",
"value": [8,9]
},
{
"key": "d",
"value": [1,2,3]
},
{
"key": "a",
"value": [4,5]
},
{
"key": "e",
"value": [3,4,5]
}
]
Is there any painless way to combine both arrays keeping all the keys and merging the values?
The result could be either
[
{
"key": "a",
"value": [1,2,3,4,5]
},
{
"key": "b",
"value": [1,2,3,8,9]
},
{
"key": "c",
"value": [1,2,3]
},
{
"key": "d",
"value": [1,2,3]
},
{
"key": "e",
"value": [3,4,5]
}
]
or
[
{
"a": [1,2,3,4,5]
},
{
"b": [1,2,3,8,9]
},
{
"c": [1,2,3]
},
{
"d": [1,2,3]
},
{
"e": [3,4,5]
}
]
Thank you!
@Paulie78 I am a newbie to PowerAutomate, and I am not sure how to import your flow JSON? Can you point me in the right direction?
EDIT: I found the answer. Easy, but not intuitive 🙂
nvm I found it!
The value of combined comes out as an array of values, which end up in newArray. Lots of of the actions in your apply to each loop will be doing the same.
@Paulie78, your solution works perfectly, and in addition, it contains a new concept for me.
On the last step inside the eachKey loop, the step "Combined" creates the object with the key/values, and outside the loop (step newArray) contains all of them together afterwards.
How come the "newArray" gathers all the data? Isn't the value of "Combined" overwritten on every loop?
In any coding language, I'd expect the "newArray" to contain only the result of the last interaction.
Could you please give me some background or source for this concept?
Was an interesting question, this does what you want, copy and paste again:
{"id":"cec54acd-d2f5-4d1c-8b4a-b094-60172b38","brandColor":"#8C3900","connectionReferences":{},"connectorDisplayName":"Control","icon":"data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIiIGhlaWdodD0iMzIiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDMyIDMyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPg0KIDxwYXRoIGQ9Im0wIDBoMzJ2MzJoLTMyeiIgZmlsbD0iIzhDMzkwMCIvPg0KIDxwYXRoIGQ9Im04IDEwaDE2djEyaC0xNnptMTUgMTF2LTEwaC0xNHYxMHptLTItOHY2aC0xMHYtNnptLTEgNXYtNGgtOHY0eiIgZmlsbD0iI2ZmZiIvPg0KPC9zdmc+DQo=","isTrigger":false,"operationName":"Combine_Arrays","operationDefinition":{"type":"Scope","actions":{"Array1":{"type":"Compose","inputs":[{"key":"a","value":[1,2,3]},{"key":"b","value":[1,2,3]},{"key":"c","value":[1,2,3]}],"runAfter":{}},"Array2":{"type":"Compose","inputs":[{"key":"a","value":[4,5]},{"key":"b","value":[8,9]},{"key":"d","value":[1,2,3]}],"runAfter":{"Array1":["Succeeded"]}},"keyValues":{"type":"Select","inputs":{"from":"@union(outputs('Array1'), outputs('Array2'))","select":"@item()['key']"},"runAfter":{"Array2":["Succeeded"]}},"Unique_Keys":{"type":"Compose","inputs":"@union(body('keyValues'),body('keyValues'))","runAfter":{"keyValues":["Succeeded"]}},"eachKey":{"type":"Foreach","foreach":"@outputs('Unique_Keys')","actions":{"filterArray2":{"type":"Query","inputs":{"from":"@outputs('Array2')","where":"@equals(item()['key'], outputs('CurrentKey'))"},"runAfter":{"array1Values":["Succeeded"]}},"CurrentKey":{"type":"Compose","inputs":"@item()","runAfter":{}},"filterArray1":{"type":"Query","inputs":{"from":"@outputs('Array1')","where":"@equals(item()['key'], outputs('CurrentKey'))"},"runAfter":{"CurrentKey":["Succeeded"]}},"Combined":{"type":"Compose","inputs":{"key":"@{outputs('CurrentKey')}","value":"@union\r\n(\r\nif(equals(outputs('array1Values'), null), outputs('EmptyArray'), outputs('array1Values')),\r\nif(equals(outputs('array2Values'), null), outputs('EmptyArray'), outputs('array2Values'))\r\n)"},"runAfter":{"array2Values":["Succeeded"]}},"array1Values":{"type":"Compose","inputs":"@first(body('filterArray1'))?['value']","runAfter":{"filterArray1":["Succeeded"]}},"array2Values":{"type":"Compose","inputs":"@first(body('filterArray2'))?['value']","runAfter":{"filterArray2":["Succeeded"]}}},"runAfter":{"EmptyArray":["Succeeded"]}},"EmptyArray":{"type":"Compose","inputs":[],"runAfter":{"Unique_Keys":["Succeeded"]}},"newArray":{"type":"Compose","inputs":"@outputs('Combined')","runAfter":{"eachKey":["Succeeded"]}}},"runAfter":{}}}
@Paulie78, I appreciate your help.
Let me play around and I'll let you know if I need your brain.
**bleep**, so it does. In that case, the answer to your question, is there is not a painless way, but there is a way, but involves an apply to each loop. Do you want me to put it together for you?
In addition to my previous answer, I just would like to add that the inputs array could be in the format below, if it's any easier:
[
{
"a": [1,2,3]
},
{
"b": [1,2,3]
},
{
"c": [1,2,3]
}
]
[
{
"b": [8,9]
},
{
"d": [1,2,3]
},
{
"a": [4,5]
},
{
"e": [3,4,5]
}
]
That's a very elegant/comprehensive/efficient solution, but it misses the extra key "d" and "e" present in the second array.
The two original arrays will contain "keys" that are both common and unique among them. The common ones must be merged, and the unique ones (from both) must be kept.
Also, the number of keys will not be the same nor in the same order, so I don't think using index will be possible in this case (I edited the original question to make it more clear).
Michael E. Gernaey
566
Super User 2025 Season 1
David_MA
516
Super User 2025 Season 1
stampcoin
492