I have below two arrays.
# My First request
$MyResultArray1 = Invoke-RestMethod -Uri "http://myhost/rest-api-1" -Method GET -ErrorAction 'Stop'
# My Second request
$MyResultArray2 = Invoke-RestMethod -Uri "http://myhost/rest-api-2" -Method GET -ErrorAction 'Stop'
Both the request returns the JSON Array something like Below.
#Array 1 - $MyResultArray1
{
"values": [
{
"id": 1,
"testType": "something1"
},
{
"id": 2,
"testType": "something2"
},
{
"id": 3,
"testType": "something3"
}
]
}
#Array 2 - $MyResultArray2
{
"values": [
{
"testId": 1,
"testResult": "result1",
"testMeta": {}
},
{
"testId": 2,
"testResult": "result2",
"testMeta": {}
},
{
"testId": 3,
"testResult": "result3",
"testMeta": {}
}
]
}
I want to merge these JSON array based on the ID
and generate the resulting array as below.
#Array 3 - $Result
{
"result": [
{
"id": 1, <-- From Array#1
"testType": "something1" <-- From Array#1
"testResult": "result1" <-- From Array#2
},
{
"id": 2,
"testType": "something2"
"testResult": "result2"
},
{
"id": 3,
"testType": "something3"
"testResult": "result3"
},
]
}
I want to match the testId
with id
and generate the resulting array. I want to search for the $MyResultArray2.testId
in the $MyResultArray1
and add the new field "testResult" in $MyResultArray1
.
I am very new to Powershell, I am not sure how it is done in Powershell.
I able to iterate array but not able to search and create new object.