0
0
PowershellConversionBeginner · 2 min read

PowerShell Script to Convert CSV to JSON Easily

Use Import-Csv 'input.csv' | ConvertTo-Json | Set-Content 'output.json' in PowerShell to convert a CSV file to JSON format quickly.
📋

Examples

InputName,Age Alice,30 Bob,25
Output[ { "Name": "Alice", "Age": "30" }, { "Name": "Bob", "Age": "25" } ]
InputProduct,Price,Stock Pen,1.5,100 Notebook,3.0,200
Output[ { "Product": "Pen", "Price": "1.5", "Stock": "100" }, { "Product": "Notebook", "Price": "3.0", "Stock": "200" } ]
InputCity,Population New York,8419000 Los Angeles,3980000 Chicago,2716000
Output[ { "City": "New York", "Population": "8419000" }, { "City": "Los Angeles", "Population": "3980000" }, { "City": "Chicago", "Population": "2716000" } ]
🧠

How to Think About It

To convert CSV to JSON in PowerShell, first read the CSV file using Import-Csv which converts each row into an object. Then use ConvertTo-Json to transform these objects into JSON format. Finally, save the JSON string to a file with Set-Content.
📐

Algorithm

1
Read the CSV file using Import-Csv to get objects.
2
Convert the objects to JSON format using ConvertTo-Json.
3
Write the JSON string to an output file using Set-Content.
💻

Code

powershell
Import-Csv 'input.csv' | ConvertTo-Json | Set-Content 'output.json'

# To see the JSON output on screen:
Import-Csv 'input.csv' | ConvertTo-Json | Write-Output
Output
[ { "Name": "Alice", "Age": "30" }, { "Name": "Bob", "Age": "25" } ]
🔍

Dry Run

Let's trace converting a CSV with two rows: Name,Age\nAlice,30\nBob,25 through the code.

1

Import CSV

Import-Csv reads the CSV and creates an array of objects: [{Name: 'Alice', Age: '30'}, {Name: 'Bob', Age: '25'}]

2

Convert to JSON

ConvertTo-Json converts the array of objects into a JSON string representing the array.

3

Write JSON to file

Set-Content writes the JSON string to 'output.json' file.

StepData
Import-Csv[{Name: 'Alice', Age: '30'}, {Name: 'Bob', Age: '25'}]
ConvertTo-Json[{"Name":"Alice","Age":"30"},{"Name":"Bob","Age":"25"}]
Set-ContentWrites JSON string to output.json
💡

Why This Works

Step 1: Import-Csv reads CSV

The Import-Csv cmdlet reads the CSV file and converts each row into a PowerShell object with properties matching the CSV headers.

Step 2: ConvertTo-Json transforms objects

ConvertTo-Json takes the array of objects and converts it into a JSON formatted string representing the data.

Step 3: Set-Content saves JSON

Set-Content writes the JSON string to a file, creating or overwriting the output JSON file.

🔄

Alternative Approaches

Using Out-File instead of Set-Content
powershell
Import-Csv 'input.csv' | ConvertTo-Json | Out-File 'output.json' -Encoding utf8
Out-File allows specifying encoding explicitly, useful for UTF-8 output.
Using Get-Content with ConvertFrom-Csv
powershell
$csv = Get-Content 'input.csv' | ConvertFrom-Csv
$csv | ConvertTo-Json | Set-Content 'output.json'
Reads CSV as text then converts; useful if you want to manipulate CSV lines before conversion.
Using -Depth parameter with ConvertTo-Json
powershell
Import-Csv 'input.csv' | ConvertTo-Json -Depth 3 | Set-Content 'output.json'
Increase depth if CSV has nested objects or complex data structures.

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

Time Complexity

The script reads each CSV row once and converts it to JSON, so time grows linearly with the number of rows.

Space Complexity

Memory usage grows linearly with the number of CSV rows because all data is loaded into memory before conversion.

Which Approach is Fastest?

Using Import-Csv piped directly to ConvertTo-Json is fastest and simplest; alternatives add overhead or complexity.

ApproachTimeSpaceBest For
Import-Csv | ConvertTo-Json | Set-ContentO(n)O(n)Simple, direct CSV to JSON conversion
Get-Content + ConvertFrom-Csv + ConvertTo-JsonO(n)O(n)When preprocessing CSV lines is needed
ConvertTo-Json with -Depth parameterO(n)O(n)Handling nested or complex CSV data
💡
Use ConvertTo-Json -Depth 3 if your CSV data has nested objects to avoid truncated JSON output.
⚠️
Forgetting to use Import-Csv and trying to convert raw CSV text directly to JSON causes errors.