I have this function
function Get-JsonContent{
param (
[Parameter(
Mandatory = $true,
HelpMessage = "Path to the json config file."
)]
[string] $Filepath
)
$content = Get-Content -Path $Filepath -Raw
$config = $content | ConvertFrom-Json -NoEnumerate
return $config
}
That basically just reads the json, I noticed that single element json arrays get converted into an object, and this can be solved by using the -NoEnumerate parameter at least from powershell seven. By doing this however I only notice this works when I pipe the ConvertFrom-Json with Convert-ToJson
So for example
ConvertFrom-Json $var -NoEnumerate | ConvertTo-Json
However the below implementation doesn't work I for example read two json files, merge the files the convert the output to json, so not entirely sure how that would work.I could use the -Array option but I also have other objects I don't want to convert to arrays
$config =just return from$content | ConvertFrom-Json -NoEnumerateand it should preserve the array. Compare:& { $a = '[{"foo": "bar"}]' | ConvertFrom-Json -NoEnumerate; $a } | ConvertTo-Jsonversus& { '[{"foo": "bar"}]' | ConvertFrom-Json -NoEnumerate } | ConvertTo-Json,, meaning:return ,$config