Convert a CSV File to JSON Format in Python

To convert a CSV file to JSON format in Python, we can use the built-in csv module to read the CSV file and the json module to write it as a JSON file. This conversion is useful for working with APIs, databases, and structured data manipulation.


Examples to Convert a CSV File to JSON Format

1. Converting a Simple CSV File to JSON

In this example, we will convert a simple CSV file students.csv with column headers into JSON format.

main.py

</>
Copy
import csv
import json

# Open the CSV file for reading
with open("students.csv", "r") as csv_file:
    csv_reader = csv.DictReader(csv_file)  # Read CSV as dictionary

    # Convert CSV data to a list of dictionaries
    data = list(csv_reader)

# Write JSON data to a file
with open("students.json", "w") as json_file:
    json.dump(data, json_file, indent=4)

print("CSV file successfully converted to JSON!")

Explanation:

  1. We import the csv and json modules to handle CSV reading and JSON writing.
  2. Using open(), we read the data.csv file.
  3. The csv.DictReader() function reads each row as a dictionary using column headers as keys.
  4. We convert the CSV data into a list of dictionaries using list(csv_reader).
  5. We open a JSON file in write mode and use json.dump() to write the data in JSON format with indentation.
  6. The JSON file is created successfully, and a confirmation message is printed.

Output:

CSV file successfully converted to JSON!

2. Converting CSV to JSON with Custom Keys

In this example, we will manually specify keys instead of using CSV column headers.

main.py

</>
Copy
import csv
import json

# Define custom keys for each column
custom_keys = ["Roll Number", "First Name", "Grade"]

# Open CSV and read data
with open("students.csv", "r") as csv_file:
    csv_reader = csv.reader(csv_file)  # Read CSV as list of rows

    # Skip the first row (column headers)
    next(csv_reader)

    # Convert CSV data to list of dictionaries
    data = [dict(zip(custom_keys, row)) for row in csv_reader]

# Write to JSON file
with open("students.json", "w") as json_file:
    json.dump(data, json_file, indent=4)

print("CSV file converted with custom keys!")

Explanation:

  1. We define a list custom_keys that contains column names.
  2. We use csv.reader() to read the file as raw row data.
  3. The first row is skipped using next(csv_reader) since we manually define keys.
  4. Each row is converted into a dictionary using zip(custom_keys, row).
  5. The resulting list of dictionaries is written to a JSON file.

Output:

CSV file converted with custom keys!

Conclusion

In this tutorial, we explored multiple ways to convert a CSV file to JSON in Python:

  1. Basic CSV to JSON conversion: Using csv.DictReader() to create a list of dictionaries.
  2. Custom Keys: Manually defining keys instead of using CSV headers.
  3. Nested JSON Structure: Creating a structured JSON output with grouped keys.

Each method provides a different approach based on your needs. The csv and json modules make it easy to handle these conversions efficiently.