I am reading from CSV file and trying to create JSON structure similar to something like this, the connections property are from 3 columns combined (namely online, mirror and shared), I am combining them into one array as connections in JSON. I don't know why I am not getting the correct format of property with an array.
{
"connectionmap": [
{
"Environment": "abc",
"connections": ["x1", "x2", "x3"]
},
{
"Environment": "def",
"connections": ["y1"]
}
]
}
Currently, my code is this:
function connectionPointsGenerator {
Param(
[Parameter(Mandatory = $true)]
[string]$CsvPath,
[Parameter(Mandatory = $true)]
[string]$OutPath
)
$Json = Import-Csv $CsvPath | ConvertTo-Json
$JsonObject = $Json | ConvertFrom-Json
foreach ($env in $JsonObject) {
[array]$connections = @()
$connections += "$($env.online)"
if ($env.mirror) {
$connections += "$($env.mirror)"
}
if ($env.shared) {
$connections += "$($env.shared)"
}
Write-Output "$($connections.GetType())"
$env.PSObject.Properties.Remove("online")
$env.PSObject.Properties.Remove("mirror")
$env.PSObject.Properties.Remove("shared")
$env | Add-Member -MemberType NoteProperty -Name "connections" -Value $connections -PassThru | Out-Null
}
$hash = @{}
$hash.Add("connectionmap",$JsonObject)
$hash.connectionPointMapping.dit
$FinalJson = $hash | ConvertTo-Json | Out-File $OutPath
}
This is what I am getting:
{
"connectionmap": [
{
"Environment": "abc",
"connections": "x1 x2 x3"
},
{
"Environment": "def",
"connections": "y1"
}
]
}