Skip to main content

Notifications

Community site session details

Community site session details

Session Id : 1RLMvKoCzj7/5ujni9U6B4
Power Automate - Building Flows
Answered

Merging two Arrays of objects

Like (2) ShareShare
ReportReport
Posted on 9 Feb 2022 13:06:44 by

Hi

I am trying to merge two arrays of objects to one array.
The two arrays have one property in common that should be used to correlate the data correctly.
The arrays are not sorted in order and have different number of objects.

 

How do I combine the two arrays into to a single array with three properties using the ID property to correlate them correctly?

Br Martin Strandholm

Array 1:

[
{
"ID": "3333",
"Object_Name": "Power"
},
{
"ID": "1111",
"Object_Name": "Automate"
},
{
"ID": "2222",
"Object_Name": "Function"
}
]

 

Array 2:

 

[
{
"ID": "1111",
"Size": "5"
},
{
"ID": "2222",
"Size": "6"
},
{
"ID": "3333",
"Size": "8"
},

{
"ID": "4444",
"Size": "8"
}
]

  • sree813 Profile Picture
    75 on 17 May 2024 at 07:44:39
    Re: Merging two Arrays of objects

    Hey @JAWL, I was able to do it in my solution. Here are my two addproperty values which is converted into single expression.


    addProperty(item(), 'fine_band', xpath(xml(outputs('XML')), concat('//Array[transaction_id/text()="', item()?['transaction_id'], '"]/fine_band/text()'))?[0])

    addProperty(item(), 'transaction_reference', xpath(xml(outputs('XML')), concat('//Array[transaction_id/text()="', item()?['transaction_id'], '"]/transaction_reference/text()'))?[0])


    Single Expression:

    addProperty(addProperty(item(),'fine_band',xpath(xml(outputs('XML')), concat('//Array[transaction_id/text()="', item()?['transaction_id'], '"]/fine_band/text()'))?[0]),'transaction_reference', xpath(xml(outputs('XML')), concat('//Array[transaction_id/text()="', item()?['transaction_id'], '"]/transaction_reference/text()'))?[0])



    Hope this helps you.
    Thanks.



     

  • Paulie78 Profile Picture
    8,407 Super User 2025 Season 1 on 22 Jan 2024 at 10:12:18
    Re: Merging two Arrays of objects

    Did you try testing the xpath query in xpather.com ?

  • JAWL Profile Picture
    391 on 22 Jan 2024 at 01:57:04
    Re: Merging two Arrays of objects

    @Paulie78 , is there a way to add two properties within a single Select action?

  • OJBridger Profile Picture
    32 on 08 May 2023 at 17:23:14
    Re: Merging two Arrays of objects

    Sorry for resurrecting an old thread!

     

    I'm having a problem in matching the IDs across the 2 Arrays I have. I've followed your guide to a T, and can't get it to work. I included the '?[0]' in the addProperty command, and it just fills every single item with 'null'

     

    I know for a fact that the ID is present in both arrays, I can even see it on the output of each 'Select' step. The ID I am using is a date concatenated with a unique order number, so: 04/30/2023869. This unique ID is 100% definitely in both arrays and I cannot get it to match. Would the string format be causing this error, maybe the '/'? I'm at a loss!

  • 20Nails Profile Picture
    2 on 08 Mar 2023 at 02:02:57
    Re: Merging two Arrays of objects

    Try to duplicate the steps.

    -Just add a PrepareArray1 action:
    {
    "root": {
    "Array": @{outputs('Array1')}
    }
    }

    -Add a XML2 action with the ouputs of the PrepareArray1 action.
    xml(outputs('PrepareArray1'))

    -Then add a parallel branch and a Select2 action to add the property present in Array1 that is missing in Array2 (in this case, the Object_Name property):
    FROM: outputs('Array2')
    MAP: addProperty(item(), 'Object_Name', xpath(outputs('XML2'), concat('//Array[ID/text()="', item()?['ID'], '"]/Object_Name/text()'))?[0])

    -Then, add a compose action to union both select1 and select2
    union(outputs('Select'),outputs('Select2')

    This step will merge both arrays without duplicates.

    Here the complete flow.

    Part1Part1

     

    Part2Part2

     




  • svivek Profile Picture
    65 on 22 Nov 2022 at 13:44:12
    Re: Merging two Arrays of objects

    Hi @Paulie78 

     

    Thanks for your solution. However I am having one additional query. Kindly help.

     

    Your solution added size for ID: "1111", "2222" and "3333". 

    However the fourth ID "4444" is NOT showing in the final result. Pls let me know if there is any way to have ID "4444" also included with size as final result as mentioned below:

    Like below:

     

    "ID": "4444",
    "Object_Name": "Null"

    "Size": "8"

  • svivek Profile Picture
    65 on 21 Jul 2022 at 10:49:04
    Re: Merging two Arrays of objects

    Hi @Paulie78 

    Thanks for above response it rectified the problem . However the final output value for one item is coming as null. Pls advice.

     

    Following expression used:

    xpath(outputs('XML'),concat('//Array[Parameter/text()="', item()?['ID'], '"]/No_Match_Array_1/text()'))?[0])

     

    Following is the flow:

     

    svivek_5-1658400510493.png

     

     

    Output of No match array table 2:

    svivek_4-1658400293889.png

     

     

     Output of No match array table 1:

     

    svivek_2-1658399931442.png

     

     

     

    Final output:
    svivek_0-1658399793656.png

     

  • Paulie78 Profile Picture
    8,407 Super User 2025 Season 1 on 21 Jul 2022 at 07:51:14
    Re: Merging two Arrays of objects

    You need to use the output of the action Root for XML as the inputs for the XML action. You have used No_match_Array_1_Table

  • svivek Profile Picture
    65 on 21 Jul 2022 at 02:21:06
    Re: Merging two Arrays of objects

    Hi,

    I applied above solution in my case which is:

    I need to merge following arrays:

    Array 1:

    [
      {
        "Parameter""2A",
        "No Match Array 1""40"
      }
    ]
     
    Array 2:
    [
      {
        "Parameter""2A",
        "No Match Array 2""80"
      }
    ]
    I require final array as
    [
      {
        "Parameter""2A",
        "No Match Array 1""40"
         "No Match Array 2": "80"
      }
    ]
     
     
    For this I created following flow. Here both arrays are coming from select operation:
    svivek_0-1658367910556.png

     

    However I am getting following error . Pls advice where am I going wrong. 

     

    svivek_1-1658370008288.png

     

  • Paulie78 Profile Picture
    8,407 Super User 2025 Season 1 on 10 Feb 2022 at 15:55:45
    Re: Merging two Arrays of objects

    Change the expression to:

    addProperty(item(), 'Size', xpath(outputs('XML'), concat('//Array[ID/text()="', item()?['ID'], '"]/Size/text()'))?[0])

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,745 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,091 Most Valuable Professional

Leaderboard
Loading started