5 Easy Steps to Convert a CSV to a JSON File
You can convert a CSV file to a JSON file by using the following five steps:
- Import the
csv
andjson
libraries - Open the CSV as a file object in reading mode using the
open(path_to_csv, 'r')
function in a context manager (=with
environment). - Load the CSV content into Python using the
csv.DictReader(fobj)
and pass the file object just created. - Iterate over each
row
and update a newly-created dictionarymy_json
using one of the column values as key:my_json[key] = row
- Store the
my_json
dictionary data in a JSON file using thejson.dumps(my_json)
function.
Here’s a code example that converts the CSV file 'my_file.csv'
to a JSON file 'my_file.json'
:
import csv import json csv_file = 'my_file.csv' json_file = 'my_file.json' my_json = {} with open(csv_file, 'r') as fobj: reader = csv.DictReader(fobj) for row in reader: # Use one of the CSV column names as a key key = row['Name'] my_json[key] = row with open(json_file,'w') as fobj: fobj.write(json.dumps(my_json, indent=2))
Note that you should use one of the CSV columns as a key. In this example, we used the column ‘Name
‘ from our CSV file. In your case, you’ll need to use your own column headers.
Input CSV File:
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
Output JSON File:
{
"Alice": {
"Name": "Alice",
"Job": "Programmer",
"Age": "23",
"Income": "110000"
},
"Bob": {
"Name": "Bob",
"Job": "Executive",
"Age": "34",
"Income": "90000"
},
"Carl": {
"Name": "Carl",
"Job": "Sales",
"Age": "45",
"Income": "50000"
}
}
You can try it yourself in our interactive Jupyter Notebook here:
Python Convert CSV to JSON with Headers
The csv.DictReader()
approach presented before assumes that the first line in the CSV is the header. Thus, the first line is used as a header of the CSV — for example, to access the specific row key of the column 'Name'
using row['Name']
.
Python Convert CSV to JSON without Headers
The csv.DictReader()
approach presented before assumes that the first line in the CSV is the header.
If your CSV file doesn’t have any header information in the first line, you can set them manually using the fieldnames
argument of the csv.DictReader()
method that takes a sequence of column labels (strings).
Here’s the syntax of the DictReader()
method:
class csv.DictReader(f, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)
“The fieldnames
parameter is a sequence. If fieldnames
is omitted, the values in the first row of file f
will be used as the fieldnames
.” — Documentation
The following code snippet shows how to convert the same CSV without headers to a JSON file by setting custom column (header) strings as a list ['XXX', 'YYY', 'ZZZ', '---']
:
import csv import json csv_file = 'my_file.csv' json_file = 'my_file.json' my_json = {} with open(csv_file, 'r') as fobj: reader = csv.DictReader(fobj, fieldnames=['XXX', 'YYY', 'ZZZ', '---']) for row in reader: key = row['---'] my_json[key] = row with open(json_file,'w') as fobj: fobj.write(json.dumps(my_json, indent=2))
Here are the input and output files:
Input CSV File:
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
Output JSON File:
{
"Income": {
"XXX": "Name",
"YYY": "Job",
"ZZZ": "Age",
"---": "Income"
},
"110000": {
"XXX": "Alice",
"YYY": "Programmer",
"ZZZ": "23",
"---": "110000"
},
"90000": {
"XXX": "Bob",
"YYY": "Executive",
"ZZZ": "34",
"---": "90000"
},
"50000": {
"XXX": "Carl",
"YYY": "Sales",
"ZZZ": "45",
"---": "50000"
}
}
Or, as a screenshot:
You can see that the first row is now used as a normal CSV entry row and not as a header as in the previous example without specifying the fieldnames
argument of the DictReader()
method.
Python Convert CSV to JSON Pandas
The pandas.to_json()
method converts a DataFrame object to a JSON string.
The syntax for this method is as follows:
# Syntax to_json() DataFrame.to_json(path_or_buf=None, orient=None, date_format=None, double_precision=10, force_ascii=True, date_unit='ms', default_handler=None, lines=False, compression='infer', index=True, indent=None, storage_options=None)
The following example reads in the countries.csv
file to a DataFrame. This DataFrame then converts to JSON:
df = pd.read_csv('countries.csv').head() result = df.to_json(indent=4, orient='records', lines=True) print(result)
- Line [1] reads in the first five (5) rows (head) of the
countries.csv
file. The output saves to a DataFrame (df
). - Line [2] does the following:
- converts the DataFrame to a JSON format
- formats the output by indenting each record four (4) spaces from the left
- sets the orient parameter to records and lines to
True
(see above definition). - saves the output to
result
.
- Line [3] outputs the result to the terminal.
Output – result
{ "Country":"Germany", "Capital":"Berlin", "Population":83783942, "Area":357021 } |
{ "Country":"France", "Capital":"Paris", "Population":67081000, "Area":551695 } |
{ "Country":"Spain", "Capital":"Madrid", "Population":47431256, "Area":498511 } |
{ "Country":"Italy", "Capital":"Rome", "Population":60317116, "Area":301338 } |
{ "Country":"Poland", "Capital":"Warsaw", "Population":38383000, "Area":312685 } |
More Python CSV Conversions
🐍 Learn More: I have compiled an “ultimate guide” on the Finxter blog that shows you the best method, respectively, to convert a CSV file to JSON, Excel, dictionary, Parquet, list, list of lists, list of tuples, text file, DataFrame, XML, NumPy array, and list of dictionaries.
Pheww! Let’s end this article on a more funny note! 🙂
Programmer Humor
Q: How do you tell an introverted computer scientist from an extroverted computer scientist?
A: An extroverted computer scientist looks at your shoes when he talks to you.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.