I have the following inputs - 2 json files one is the base one and the second contains the same properties but the different values, I\'d like to merge that objects.
If you know the names of the elements (per your example above), you could do it explicitly like this:
$Json1 ='{
a: {
b:"asda"
},
c: "asdasd"
}
' | ConvertFrom-Json
$Json2 = '{
a:{
b:"d"
}
}
' | ConvertFrom-Json
$Json1.a = $Json2.a
Result:
$Json1 | ConvertTo-Json
{
"a": {
"b": "d"
},
"c": "asdasd"
}
If you're looking for something that will merge the two without knowing the explicit key name, you could do something like the following. This will essentially overwrite any properties in the first Json with those from the second Json, where they are duplicated at the first level (it won't seek matches in the nested properties and again this is an overwrite not a merge):
$Json2.psobject.Properties | ForEach-Object {
$Json1 | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value -Force
}