Sitemap

Convert CSV to JSON with Python

2 min readJan 7, 2019

--

I got help from a youtube tutorial linked below

  1. import csv and import json packages
  2. Create a file path to your CSV file: csvFilePath = ‘csv_file_name.csv’
  3. Create a file path to your JSON file: jsonFilePath = ‘json_file_name.json’
  4. Create an empty dictionary to store your converted JSON data: data = {}
  5. Open and read the CSV:
Open and read CSV file

7. Create a JSON file with the file path from step 3 to write converted data on

Press enter or click to view image in full size
Create new JSON file and write on it

Final code snippet

Press enter or click to view image in full size
Final code example

Note to self:

Dictionaries — maps a set of objects (keys) to another set of objects (values).

  • Use {} to create a dictionary and [] to index it.
  • Separate key and value pairs with : and commas between each key pair

YouTube tutorial — thank you ProProgramming101

--

--

Responses (5)