I have two JSON objects and I am trying to build one object out of it. I am using an example here, but the properties/names are dynamic.
I have looked into JOIN function, MERGE, Combine-Object none of them seems to do what I want. As they either replace the values with the one or give me random results.
This is my example,
$aVar = '{ "oldEmployees" : [ { "firstName": "Jane", "lastName": "Doe" }, { "firstName": "John", "lastName": "Doe" } ] }'
$bVar = '{ "newEmployees" : [ { "firstName": "Joe", "lastName": "Doe" } ] }'
I would like to have one JSON as,
{
"oldEmployees": [
{
"firstName": "Jane",
"lastName": "Doe"
},
{
"firstName": "John",
"lastName": "Doe"
}
],
"newEmployees": [
{
"firstName": "Joe",
"lastName": "Doe"
}
]
}
BACKGROUND: I am using a JSON object to deploy multiple resources in Azure DevOps pipeline using an ARM template. I have hit a point where I am limited to use Powershell script to combine multiple variables due to the size limit on the Azure Pipeline Variables to a max of 4K characters.
($aVar | ConvertFrom-Json) |Join-Object($bVar | ConvertFrom-Json) | ConvertTo-Json -Compress→{"oldEmployees":[{"firstName":"Jane","lastName":"Doe"},{"firstName":"John","lastName":"Doe"}],"newEmployees":{"firstName":"Joe","lastName":"Doe"}}(See also: Merge two json objects)