Hi, I have a question, maybe someone has an idea.
I’m reading two JSON files and would like to merge the information into one PSCustomObject but only the single values should be changed if the information already exists or a new propertiy shoud be added if the property doesent exist.
$Json1 is a global information and the values of $Json2 have the last word if there are equal keys or if additional keys musst be added.
So the information from $Json2 is leading, but should only overwrite or enrich information.
Example of JSONs.
$Json1:
{
“Hostname”: “Server1”,
“Port”: "“5001”,
“Paths”:{
“Path1”: “C:\test\123”,
“Path2”: “C:\test\456”
“Path3”: “C:\test\456”
“Pfad4”: “C:\test\456”
}
}
$Json2:
{
“Hostname”: “Server2”,
“Port”: "“5001”,
“Paths”:{
“Path1”: “E:\Prod\123”,
“Path2”: “E:\Prod\456”
}
}
I read the two files and convert it using ConvertFrom-Json
So the information is now in the variables $Json1 and $Json2
I now merge the two variables into one object.
Code:
$Object = [ordered] @{}
foreach ($Property in $Json1.PSObject.Properties) {
$Object += @{$Property.Name = $Property.Value}
}
foreach ($Property in $JSON2.PSObject.Properties) {
try{
$Object += @{$Property.Name = $Property.Value}
}catch{
$Object.$($Property.Name) = $Property.Value
}
}
}
The result of the newly created object should look as follows:
“Hostname”: “Server2”
“Port”: "“5001”,
“Paths”:@{
“Path1”: “E:\Prod\123”
“Path2”: “E:\Prod\456”
“Path3”: “C:\test\456”
“Path4”: “C:\test\456”}
But what comes out is:
“Hostname”: “Server2”
“Port”: "“5001”,
“Paths”:@{
“Path1”: “E:\Prod\123”
“Path2”: “E:\Prod\456”
}
I dont understand how to modfy only the Path1 and Path2 but keeping Path3 and 4 from the JSON1
The hostname from the JSON2 was taken over and the port remained unchanged as desired.
Only the values of path1 and path2 were changed with the JSON2 information, but path3 and path4 were deleted.
The code above deletes exactly this information path3 and path4. Everything else is replaced correctly.
So I have problems to adjust only the nested values. Everything on the first level works, but as soon as another level is added, Path-> Path1 the information of Path is completely replaced.
This is quite clear in the code, but I do not really have the idea how to solve the problem and only change the information of path1 and 2 but not deleting path3 and Path4 from the $json1
Result should look like this.
The result of the newly created object should look like this:
“Hostname”: “Server2”
“Port”: "“5001”,
“Paths”:@{
“Path1”: “E:\Prod\123”
“Path2”: “E:\Prod\456”
“Path3”: “C:\test\456”
“Path4”: “C:\test\456”}
I could access the path information with the name $Object.Paths.Path1, but the script should be generic because the use case is just to merge different Json files into one hash table/PSObject and if the same property is present in both Json files, the desired Json file information (json2) overwrites the values of the other one, regardless of the level depth. (Paths->;Path1…)
for ideas I would be thankful
best regards
Rolf