The output of ConvertFrom-Json
is single array being passed down the pipeline as such. This is a consequence of how ConvertFrom-Json
is writing it's output. It's probably collecting everything and outputting the array in one Write-Output
command (implicit or otherwise) instead of streaming each object down the pipeline as they are created. It's probably a consequence of how the cmdlet is written and may have been necessary.
Your current code is likely generating a csv like:
"Count","Length","LongLength","Rank","SyncRoot","IsReadOnly","IsFixedSize","IsSynchronized"
"5","5","5","1","System.Object[]","False","True","False"
These are the properties of the array not the objects in it. One way to get around it just park the data in a variable:
$Json = "name,data
Play,http://{gho}.domain.com/
BDomain,domain.com
Charts,2
Compress,0
CompressJ,0" |
ConvertFrom-Csv |
ConvertTo-Json
# Now you can convert back to csv without issue:
$Json = $Json | ConvertFrom-Json
$Json | ConvertTo-Csv -NoTypeInformation
Note: if you send $Json directly like $Json | ConvertFrom-Json | ConvertTo-Csv...
you may have the same issue.
Iterating over the Json objects also seems to work:
$Json = "name,data
Play,http://{gho}.domain.com/
BDomain,domain.com
Charts,2
Compress,0
CompressJ,0" |
ConvertFrom-Csv |
ConvertTo-Json
$Json |
ConvertFrom-Json |
ForEach-Object{ $_ } |
ConvertTo-Csv -NoTypeInformation
Thanks to @notjustme , you can shorten this using (...)
like:
($json | ConvertFrom-Json) | ConvertTo-Csv
Note: Corrected per mklement0's answer.
All of these options are essentially placing the array in-front of the pipe to bypass the internal behavior of ConvertFrom-Json
Again not 100% sure why ConvertFrom-Json behaves this way. I'll update with additional information when I find a more formal explanation.