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

Help creating logic scripts for Air Manager Instruments

Moderators: Ralph, russ

Post Reply
Message
Author
SimPassion
Posts: 3558
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 30 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 29 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)
Last edited by SimPassion on Thu Jun 23, 2022 1:49 pm, edited 3 times in total.

User avatar
Keith Baxter
Posts: 3629
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