Consolidate REST API resultset object

I have 5 rest method result set stored in separate object as call required separate API keys.

$apiKey1 = “SomeKey”
$apiKey2 = “SomeKey”
$resource = “http://localhost/api/books
$List1=Invoke-RestMethod -Method Get -Uri $resource -Header @{ “X-ApiKey” = $apiKey1 }
$List2=Invoke-RestMethod -Method Get -Uri $resource -Header @{ “X-ApiKey” = $apiKey2 }

$List1, $List2… have same pattern data in json, how to consolidate in single object say $AllResults?

Take a look at this example using hashtables and a lookup table. I’ll assume there is one property that is unique and shared between the datasets. This is what we will use as the lookup key. This is just to show you an example one way you can do this. If you have more properties than you want to type out, I’m sure there’s an easy way to walk through each property of each list and either create that property or add to it.

$list1 = [pscustomobject]@{
    key = 'a'
    prop2 = 2
    prop3 = @{
        prop3a = 'a'
        prop3b = 'b'
    }
}
$list2 = [pscustomobject]@{
    key = 'a'
    prop5 = 3
    prop6 = @{
        prop6c = 'c'
        prop6d = 'd'
    }
}

$lookup = $List1 | group-object -AsHashTable -Property key -AsString

$allresults = $list2 | ForEach-Object {

    $list1row = $lookup[$_.key]

    [pscustomobject]@{
        Key = $_.key
        prop2 = $list1row.prop2
        prop3 = $list1row.prop3
        prop5 = $_.prop5
        prop6 = $_.prop6
    }
}
$allresults
$allresults | ConvertTo-Json

I hope this helps.

Thank you. it helped.