Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Automate - Building Flows
Answered

Combine array of objects with common keys

(0) ShareShare
ReportReport
Posted on by 31

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!

  • subh20 Profile Picture
    3 on at
    Re: Combine array of objects with common keys

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

  • nichlas Profile Picture
    2 on at
    Re: Combine array of objects with common keys

    nvm I found it!

  • Paulie78 Profile Picture
    8,416 Super User 2025 Season 1 on at
    Re: Combine array of objects with common keys

    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.

  • cmilanelo Profile Picture
    31 on at
    Re: Combine array of objects with common keys

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

     

    cassio_milanelo_0-1618857298833.png

     

  • cmilanelo Profile Picture
    31 on at
    Re: Combine array of objects with common keys

    @Paulie78 you're sick! 😊

     

    That worked like a charm.

    Thank you so much! 

  • Verified answer
    Paulie78 Profile Picture
    8,416 Super User 2025 Season 1 on at
    Re: Combine array of objects with common keys

    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":{}}}
  • cmilanelo Profile Picture
    31 on at
    Re: Combine array of objects with common keys

    @Paulie78, I appreciate your help.

    Let me play around and I'll let you know if I need your brain.

  • Paulie78 Profile Picture
    8,416 Super User 2025 Season 1 on at
    Re: Combine array of objects with common keys

    **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?

  • cmilanelo Profile Picture
    31 on at
    Re: Combine array of objects with common keys

    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]
     }
    ]
  • cmilanelo Profile Picture
    31 on at
    Re: Combine array of objects with common keys

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

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

Michael Gernaey – Community Spotlight

We are honored to recognize Michael Gernaey as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 566 Super User 2025 Season 1

#2
David_MA Profile Picture

David_MA 516 Super User 2025 Season 1

#3
stampcoin Profile Picture

stampcoin 492