0
0
PowershellConversionBeginner · 2 min read

PowerShell Script to Convert JSON to CSV Easily

Use Get-Content input.json | ConvertFrom-Json | Export-Csv output.csv -NoTypeInformation in PowerShell to convert a JSON file to CSV format.
📋

Examples

Input[{"name":"Alice","age":30},{"name":"Bob","age":25}]
Outputname,age Alice,30 Bob,25
Input[{"product":"Pen","price":1.5,"stock":100},{"product":"Notebook","price":3.0,"stock":200}]
Outputproduct,price,stock Pen,1.5,100 Notebook,3,200
Input[]
Output
🧠

How to Think About It

To convert JSON to CSV in PowerShell, first read the JSON data and convert it into PowerShell objects using ConvertFrom-Json. Then export these objects to CSV format using Export-Csv. This approach works because JSON objects map naturally to CSV rows and columns.
📐

Algorithm

1
Read JSON content from a file or string
2
Convert JSON text into PowerShell objects using ConvertFrom-Json
3
Export the objects to a CSV file using Export-Csv with -NoTypeInformation to avoid extra type info
4
Save or display the CSV output
💻

Code

powershell
Get-Content input.json | ConvertFrom-Json | Export-Csv output.csv -NoTypeInformation
Write-Output "CSV file created as output.csv"
Output
CSV file created as output.csv
🔍

Dry Run

Let's trace converting JSON array of two people to CSV

1

Read JSON file

[{"name":"Alice","age":30},{"name":"Bob","age":25}]

2

Convert JSON to objects

@{name=Alice; age=30}, @{name=Bob; age=25}

3

Export objects to CSV

name,age Alice,30 Bob,25

StepData
1[{"name":"Alice","age":30},{"name":"Bob","age":25}]
2@{name=Alice; age=30}, @{name=Bob; age=25}
3name,age Alice,30 Bob,25
💡

Why This Works

Step 1: Read JSON content

The script reads the JSON text from a file using Get-Content, which loads the raw JSON string.

Step 2: Convert JSON to objects

ConvertFrom-Json parses the JSON string into PowerShell objects that can be manipulated or exported.

Step 3: Export to CSV

Export-Csv takes the objects and writes them as CSV rows and columns, with -NoTypeInformation to keep the file clean.

🔄

Alternative Approaches

Manual parsing and writing CSV
powershell
$json = Get-Content input.json | ConvertFrom-Json
$csv = $json | ForEach-Object { "$($_.name),$($_.age)" }
$csv | Set-Content output.csv
Write-Output "CSV file created manually"
This method is less flexible and requires manual column handling but can be useful for custom CSV formats.
Using ConvertTo-Csv with Out-File
powershell
$json = Get-Content input.json | ConvertFrom-Json
$json | ConvertTo-Csv -NoTypeInformation | Out-File output.csv
Write-Output "CSV file created using ConvertTo-Csv"
This approach uses ConvertTo-Csv to convert objects to CSV text and writes it to a file; similar to Export-Csv but more manual.

Complexity: O(n) time, O(n) space

Time Complexity

The script processes each JSON object once to convert and export, so time grows linearly with the number of objects.

Space Complexity

Memory usage grows linearly with the JSON size as all objects are loaded into memory before exporting.

Which Approach is Fastest?

Using Export-Csv directly after ConvertFrom-Json is efficient and simple; manual parsing is slower and error-prone.

ApproachTimeSpaceBest For
ConvertFrom-Json + Export-CsvO(n)O(n)General use, simple and fast
Manual parsing and writingO(n)O(n)Custom CSV formats, small data
ConvertTo-Csv + Out-FileO(n)O(n)When needing CSV as string before saving
💡
Always use -NoTypeInformation with Export-Csv to avoid extra type info lines in your CSV file.
⚠️
Forgetting to use ConvertFrom-Json before exporting causes errors because raw JSON text cannot be exported directly.