Python — A program that converts a CSV file to a JSON file
CSV (Comma Separated Values) and JSON (JavaScript Object Notation) are two popular formats used for storing and exchanging data. CSV is a simple format that stores data in a table-like structure, with each row representing a record and each column representing a field. JSON, on the other hand, is a lightweight data interchange format that stores data as key-value pairs, making it easier to work with in web applications.
Converting data from CSV to JSON format is a common task in data processing and web development. In this article, we will look at how to write a Python program that converts a CSV file to a JSON file.
Requirements
To write the program, we will use Python 3 and the built-in csv
and json
modules. These modules provide functions for reading and writing CSV and JSON files, respectively. We will also use the os
module to handle file operations.
Step 1: Read the CSV file
The first step is to read the CSV file using the csv
module. We can use the csv.DictReader
class to read the file and store each row as a dictionary. Here's the code:
import csv
with open('input.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
data = [row for row in reader]
In this code, we open the input.csv
file and create a csv.DictReader
object. We then use a list comprehension to iterate over the rows in the file and store each row as a dictionary in the data
list.
Step 2: Write the JSON file
The next step is to write the JSON file using the json
module. We can use the json.dump
function to write the data
list to a JSON file. Here's the code:
import json
import os
with open('output.json', 'w') as jsonfile:
json.dump(data, jsonfile)
In this code, we create an output.json
file and use the json.dump
function to write the data
list to the file.
Step 3: Test the program
python csv_to_json.py
This will read the input.csv
file, convert it to a JSON file, and write it to output.json
.
Example CSV and JSON files
Let’s say we have a CSV file named input.csv
with the following contents:
name,age,gender
John,30,Male
Jane,25,Female
Bob,45,Male
After running our Python program, we should have a JSON file named output.json
with the following contents:
[
{
"name": "John",
"age": "30",
"gender": "Male"
},
{
"name": "Jane",
"age": "25",
"gender": "Female"
},
{
"name": "Bob",
"age": "45",
"gender": "Male"
}
]
As you can see, each row in the CSV file is converted to a dictionary in the JSON file, with the column headers as keys and the values as values.
Conclusion
In this article, we looked at how to write a Python program that converts a CSV file to a JSON file using the built-in csv
and `json modules in Python. We first read the CSV file using the csv.DictReader
class and stored each row as a dictionary in a list. We then wrote the list to a JSON file using the json.dump
function.
Converting data from CSV to JSON format can be useful in many applications, such as web development and data analysis. Python provides built-in modules for reading and writing both CSV and JSON files, making it easy to convert between the two formats.
Code — Python-A-program-that-converts-a-CSV-file-to-a-JSON-file| Replt
If you enjoyed this article and would like to show your support, feel free to buy me a coffee! Your support is greatly appreciated and it helps me continue creating content that you love. You can make a contribution by following this link: Buy Me a Coffee. Thank you for your generosity and for being a part of this journey!