Tuesday, August 24, 2021

how to use Powershell to convert CSV file into Json format data?

 there is a built-in function in powershell cmdlet, which will allow us to convert the csv data into json format data.

first we should go to the folder where is the csv file stored. my csv file is found in this folder

C:\code\django\django-search-app\fixtures

PS C:\code\django\django-search-app\fixtures> ls

    Directory: C:\code\django\django-search-app\fixtures

Mode                 LastWriteTime         Length Name

----                 -------------         ------ ----

-a----         8/24/2021   1:26 PM         872488 books.csv

-a----         8/24/2021   1:35 PM         883612 books_title_author.csv


here is script to be run the in windows powershell and it should be executed inside the  folder

PS C:\code\django\django-search-app\fixtures> $topicsjson = import-csv .\books_title_author.csv | ConvertTo-Json

PS C:\code\django\django-search-app\fixtures> $topicsjson | Add-Content -Path "myData.json"

 $topicsjson = import-csv .\books_title_author.csv | ConvertTo-Json

 $topicsjson | Add-Content -Path "myData.json"

after the scripts is executed.

there should be a json file name myData.json within the file.

PS C:\code\django\django-search-app\fixtures> ls

    Directory: C:\code\django\django-search-app\fixtures

Mode                 LastWriteTime         Length Name

----                 -------------         ------ ----

-a----         8/24/2021   1:26 PM         872488 books.csv

-a----         8/24/2021   1:35 PM         883612 books_title_author.csv

-a----         8/24/2021   1:40 PM        2047343 myData.json





No comments:

Post a Comment