PoshBytes: Json Vs Csv
In this PoshBytes Versus episode, ConvertTo-Csv and ConvertTo-Json go head to head to see which format handles your PowerShell data best. Learn how each manages types, nesting, and readability so you can choose the right tool for your next automation.
This post is a companion for the video embedded below. Scroll down to see the code from the video.
The Basics: Flat Objects
Create object to test with
$procs = @(
[pscustomobject]@{ Name='pwsh'; Id=1234; CPU=12.3 }
[pscustomobject]@{ Name='code'; Id=2345; CPU=8.7 }
)
Convert to CSV
$procs | ConvertTo-Csv -NoTypeInformation
Convert to Json
$procs | ConvertTo-Json -Depth 2
Types: Who Remembers What?
Object types before conversion
$procs | Get-Member -MemberType NoteProperty
Object types after CSV conversion are all string
$procs | ConvertTo-Csv | ConvertFrom-Csv | Get-Member -MemberType NoteProperty
Object types after JSON conversion maintain type (but not 100%)
$procs | ConvertTo-Json | ConvertFrom-Json | Get-Member -MemberType NoteProperty
Nested Objects: “Mind the Arrays”
Create object to test with
$user = [pscustomobject]@{
Name='Alice'
Tags=@('admin','dev')
Profile=[pscustomobject]@{
City='Dallas'
Skills=@('PowerShell','Azure')
}
}
CSV struggles with arrays/objects:
$user | ConvertTo-Csv -NoTypeInformation
JSON preserves structure:
$user | ConvertTo-Json -Depth 3
When to Use Which (Speed Dating Edition)
Get objects to convert
$svc = Get-Service | Select-Object Name, Status, DisplayName -First 5
Excel-loving pipelines adore CSV:
$svc | ConvertTo-Csv | Set-Content .\services.csv
API payloads love JSON:
$svc | ConvertTo-Json -Compress | Set-Content .\services.json
• CSV = flat tables • JSON = nested structures • CSV converts values to strings, JSON preserves native types • Use -Depth with JSON for nested data; add -Compress for compact output • Use -NoTypeInformation with CSV for clean headers • CSV plays nice with Excel • JSON plays nice with APIs/configs/logging