I have two sets of json file: Parameters1.json and parameters2.json as seen below:
Parameters1.json
{
"TypeofService":"CITService",
"ServiceName":"abc",
"SCNNAME":"abc_V1.scn",
"ScheduleInterval":"Daily",
"ScheduleDay":"MON,TUE,WED,THU,FRI,SAT",
"ScheduleTime":"08:30",
"Folder_structure": "Success\\test1,Success\\test2",
"CIT_Properties": [
{
"isPassword": false,
"Property": "host1",
"Value": "xyz"
},
{
"isPassword": false,
"Property": "Port1",
"Value": "8081"
},
{
"isPassword": false,
"Property": "user1",
"Value": "testuser"
},
{
"isPassword": true,
"Property": "password1",
"Value": "12345"
}
]
}
Parameters2.json(file to be updated)
{
"TypeofService":"CITService",
"ServiceName":"abc",
"SCNNAME":"abc_V2.scn",
"ScheduleInterval":"Daily",
"ScheduleDay":"MON,TUE,WED,THU,FRI,SAT",
"ScheduleTime":"08:30",
"Folder_structure": "Success\\test1,Success\\test2",
"CIT_Properties": [
{
"isPassword": false,
"Property": "host1",
"Value": "xyz"
},
{
"isPassword": false,
"Property": "port1",
"Value": "8080"
},
{
"isPassword": false,
"Property": "user1",
"Value": "generic"
},
{
"isPassword": true,
"Property": "password1",
"Value": "56789"
},
{
"isPassword": false,
"Property": "host2",
"Value": "xyz2"
},
{
"isPassword": false,
"Property": "port2",
"Value": "8080"
},
{
"isPassword": false,
"Property": "user2",
"Value": "user2"
},
{
"isPassword": true,
"Property": "password2",
"Value": "1234567890"
}
]
}
What I am trying to achieve is that if the 'Property' in CIT_Properties matches in both the files, then the corresponding CIT_properties.value from parameters1.json should be updated in parameters2.json.
i.e if you see above: port1, user1 and password1 are common in both the files. I would like the values (8080,generic,56789 in parameters2.json) to be replaced with (8081,testuser,12345).
What I have done so far is below:
$json1 = (Get-Content "C:\Users\parameters1.json" -Raw) | Out-String | ConvertFrom-Json
$json2=(Get-Content "C:\Users\parameters2.json" -Raw) | Out-String | ConvertFrom-Json
for ($a = 0; $a -lt $json2.PsObject.properties.value.Property.length; $a++)
{
for ($b = 0; $b -lt $json1.PsObject.properties.value.Property.length; $b++)
{
if ($json2.PsObject.properties.value.Property[$a] -eq $json1.PsObject.properties.value.Property[$b])
{
$json2.PsObject.properties.value.Value[$a]= $json1.PsObject.properties.value.Value[$b]
}
}
}
I see that I am able to access the individual values but when I try setting them, it throws me an error.
Out-Stringin theGet-Contentpipeline. Nor the brackets.$json2 = Get-Content "C:\Users\parameters2.json" -Raw | ConvertFrom-Json