On the sidelines of instrument creation : Powershell corner - CSV to JSON conversion

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
SimPassion
Posts: 5336
Joined: Thu Jul 27, 2017 12:22 am

On the sidelines of instrument creation : Powershell corner - CSV to JSON conversion

#1 Post by SimPassion »

 
Quick method, fast execution, free, with no pain and before someone is asking : yes it works on any platform, Linux, MacOS, Windows, by using PowerShell Core install named PSHost on all platforms

in two line :

Code: Select all

$csv = import-csv .\my_csv_data.csv
$csv | convertto-json | out-file .\my_json_data.json

# or more universal, with "\" replaced by "/" notation

$csv = import-csv ./my_csv_data.csv
$csv | convertto-json | out-file ./my_json_data.json
image.png
image.png (4.78 KiB) Viewed 439 times

in one line only :

Code: Select all

$csv = import-csv .\my_csv_data.csv | convertto-json | out-file .\my_json_data.json

# or more universal, with "\" replaced by "/" notation

$csv = import-csv ./my_csv_data.csv | convertto-json | out-file ./my_json_data.json
image.png
image.png (3.58 KiB) Viewed 438 times

in more detailed way :

Code: Select all

$csv = import-csv .\my_csv_data.csv		# at first, we import the CSV file from the current folder (".\" before the filename), into the $csv object variable, from the ".csv" file
$json = $csv | convertto-json			# the $csv object is converted to JSON format with maintaining its current data content and the result is put into the $json object variable
$json						# this line will only display the result in the PowerShell console
$json | out-file .\my_json_data.json		# the $json content is written as output file, into the designated ".json" filename, in the current folder (".\" before the filename)

# or more universal, with "\" replaced by "/" notation

$csv = import-csv ./my_csv_data.csv		# at first, we import the CSV file from the current folder ("./" before the filename), into the $csv object variable, from the ".csv" file
$json = $csv | convertto-json			# the $csv object is converted to JSON format with maintaining its current data content and the result is put into the $json object variable
$json						# this line will only display the result in the PowerShell console
$json | out-file ./my_json_data.json		# the $json content is written as output file, into the designated ".json" filename, in the current folder ("./" before the filename)
Last edited by SimPassion on Mon Dec 12, 2022 12:07 am, edited 4 times in total.

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: On the sidelines of instrument creation : Powershell corner - CSV to JSON conversion

#2 Post by Keith Baxter »

Interesting Gilles,

Will try it .

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

Post Reply