Converting CSV to JSON in Python
Ashley Kwon (Spring 2023)
This tutorial explains how to convert a CSV file into a JSON file using a Python program. Doing so will be useful when you want to open a file that contains integer, float, and string data entries in a Unity project.
Open a new Python file and import json and csv packages. Note that these package names are in lower cases.
Initiate a dictionary object. You'll use this object to store your csv data entries. I'll refer to this dictionary object as JsonDataStorage
Open the file you want to load and iterate through each line in your csv file using the following statement. Assume fileToRead is the path to your csv file as a string.
4. In the for loop from the code snippet above, line refers to one line in your csv file. You can access its individual data entries as if you're accessing elements in a list in Python. For example, the 2nd entry in line would be line
5. Store the data you read from each line in your csv file into the dictionary object you made in 2. Since this object is a dictionary, you'll need a key and a value for each entry. These key-value pairs will differ based on how you want to store your JSON file.
6. After your for loop finishes iterating, your dictionary object should have all data you want to store. Save your dictionary object as a json file using the following lines: