How to merge three object arrays with different attributes:
I've got a challenge where I'm looking to combine three separate arrays of objects that share a common ID but have different keys into a single array.
I'm performing a data cleansing exercise where some flags on our contact records have gotten out of wack. Some customers have more than one flag that needs correcting and others just a single one. I'd like to avoid including the attributes that aren't relevant to a particular contact.
Examples of the arrays I've built up so far:
Array 1:
[
{
"contactid": "00178844-452e-ed11-9db1-00224818a628",
"prospect": true
},
{
"contactid": "2c036cc5-e0e5-ec11-bb3d-0022481869da",
"prospect": true
},
{
"contactid": "53904ebf-3dd2-ec11-a7b5-0022481869da",
"prospect": true
}
]
Array 2:
[
{
"contactid": "c89610d3-182a-ec11-b6e6-002248156fd0",
"customer": true
},
{
"contactid": "53904ebf-3dd2-ec11-a7b5-0022481869d",
"customer": false
},
{
"contactid": "2c036cc5-e0e5-ec11-bb3d-0022481869da",
"customer": true
},
{
"contactid": "d06d9d74-2540-ec11-8c62-002248159b41",
"customer": false
}
]
Array 3:
[
{
"contactid": "c89610d3-182a-ec11-b6e6-002248156fd0",
"advocate": true
},
{
"contactid": "53904ebf-3dd2-ec11-a7b5-0022481869d",
"advocate": false
}
]
Desired result:
[
{
"contactid": "00178844-452e-ed11-9db1-00224818a628",
"prospect": true
},
{
"contactid": "2c036cc5-e0e5-ec11-bb3d-0022481869da",
"prospect": true,
"customer": true
},
{
"contactid": "c89610d3-182a-ec11-b6e6-002248156fd0",
"customer": true,
"advocate": true
},
{
"contactid": "53904ebf-3dd2-ec11-a7b5-0022481869da",
"prospect": true,
"customer": false,
"advocate": false
},
{
"contactid": "d06d9d74-2540-ec11-8c62-002248159b41",
"customer": false
}
]
Ideally, I'd like to avoid for each loops where possible.
Please share any help or guidance you can!
Hi , @ciarancandy
The best way is to get this result like this:
This is my test flow:
Compose1 and Compose 2 and Compose 3 are the data for your three array.
(1)The Compose 4 action:
union(
xpath( xml( json(concat('{"root":{"item":',string(outputs('Compose')),'}}')) ) , '/root/item/contactid/text()' ),
xpath( xml( json(concat('{"root":{"item":',string(outputs('Compose_2')),'}}')) ) , '/root/item/contactid/text()' ),
xpath( xml( json(concat('{"root":{"item":',string(outputs('Compose_3')),'}}')) ) , '/root/item/contactid/text()' )
)
(2) The Select action:
if( equals(split( string(outputs('Compose')) , item() )?[1],null),null , split( split( split( string(outputs('Compose')) , item() )?[1] , ':' )?[1] , '}' )?[0] )
if( equals(split( string(outputs('Compose_2')) , item() )?[1],null),null , split( split( split( string(outputs('Compose_2')) , item() )?[1] , ':' )?[1] , '}' )?[0] )
if( equals(split( string(outputs('Compose_3')) , item() )?[1],null),null , split( split( split( string(outputs('Compose_3')) , item() )?[1] , ':' )?[1] , '}' )?[0] )
If this reply can help you , you can click mark this reply as solution (Accept solution) which can help more people, thanks in advance!
Best Regards,
Yueyun Zhang
Michael E. Gernaey
566
Super User 2025 Season 1
David_MA
516
Super User 2025 Season 1
stampcoin
492