Skip to content

Instantly share code, notes, and snippets.

@dfinke
Created February 16, 2015 14:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dfinke/2444aef8ebee03db708d to your computer and use it in GitHub Desktop.
Save dfinke/2444aef8ebee03db708d to your computer and use it in GitHub Desktop.
Using PowerShell to Convert From JSON to CSV format
function ConvertFrom-JsonToCsv {
param(
[Parameter(ValueFromPipeline)]
$json
)
Process {
($json | ConvertFrom-Json) | ConvertTo-Csv -NoTypeInformation
}
}
'[{"Column 1": "1-1","Column 2": "1-2","Column 3": "1-3","Column 4": "1-4"},{"Column 1": "2-1","Column 2": "2-2","Column 3": "2-3","Column 4": "2-4"},{"Column 1": "3-1","Column 2": "3-2","Column 3": "3-3","Column 4": "3-4"},{"Column 1": 4,"Column 2": 5,"Column 3": 6,"Column 4": 7}]' |
ConvertFrom-JsonToCsv
@jonadv
Copy link

jonadv commented Sep 22, 2020

Could you help me how to run this script in Powershell ISE?
I've come to the following steps but keep getting errors

  1. Stored the above script in location C:\Users\Bob\PowerShellTest\ConvertFrom-JsonToCsv.ps1
  2. Stored a file in the same location named MyTest.json
  3. Open powershell
  4. Type + enter: cd C:\Users\Bob\PowerShellTest
  5. Type + enter: .\ConvertFrom-JsonToCsv.ps1 .\MyTest.json

How should I call the file as an argument?

@Bassmann
Copy link

This is not a script but a Powershell function. In a script you would source that file and the ncall the function as in the example on line 12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment