I came across this post as I was having a related issue reading a JSON file that contains a top-level array with one object. PS was unrolling the array and just returning the one and only object after calling ConvertFrom-Json
.
First, I can confirm that version 7.4.6 Core of PowerShell does not have the issue of the OP, but returns the expected result of 2 from the line:
'[{a:1},{b:2}]' | ConvertFrom-Json | measure
Second, as help for others who face the same issue I did when reading a file, the correct solution is to use the -NoEnumerate
switch:
$json = Get-Content -Path $FilePath | ConvertFrom-Json -Depth 10 -NoEnumerate
Do not use the comma operator
as mentioned in a comment above, as this will produce unexpected results in files that already have more than one element in the array!