Priya R Priya R
Updated date Nov 13, 2023
In this blog, we will learn how to easily convert CSV data into JSON format using Python. Explore multiple methods to achieve this conversion.

Introduction:

Data manipulation is a fundamental aspect of data science and analysis. One common task in this field is converting data from one format to another. In this blog, we will explore the process of converting CSV data to JSON using Python. We will cover multiple methods to cater to different scenarios, providing you with the flexibility to choose the one that suits your needs best.

Method 1: Using the csv and json Libraries

The simplest method for converting CSV to JSON in Python involves using the built-in csv and json libraries. Here's a step-by-step program to achieve this:

import csv
import json

# Define the CSV and JSON file paths
csv_file = 'data.csv'
json_file = 'data.json'

# Read data from the CSV file
data = []
with open(csv_file, 'r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
        data.append(row)

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

print(f"CSV data has been successfully converted to JSON and saved in '{json_file}'.")

In this method, we first import the csv and json libraries. We specify the file paths for the CSV input file and the JSON output file. We read the data from the CSV file using csv.DictReader to parse it as a list of dictionaries, where each row is represented as a dictionary. Then, we write this data to a JSON file using json.dump, specifying an indentation of 4 for better readability.

Output:

After running the program, you'll find a JSON file named 'data.json' containing the converted data in JSON format.

Method 2: Using Pandas

Pandas is a powerful data manipulation library in Python, and it provides a convenient way to convert CSV to JSON. Here's a program using Pandas for the conversion:

import pandas as pd

# Define the CSV and JSON file paths
csv_file = 'data.csv'
json_file = 'data.json'

# Read data from the CSV file using Pandas
data = pd.read_csv(csv_file)

# Write data to the JSON file
data.to_json(json_file, orient='records', lines=True)

print(f"CSV data has been successfully converted to JSON and saved in '{json_file}'.")

In this method, we import the pandas library. Similar to Method 1, we specify the file paths for the CSV input file and the JSON output file. Using Pandas, we read the CSV data into a DataFrame, which is a powerful data structure for data manipulation. We then use the to_json method to save the DataFrame as a JSON file, specifying the 'records' orientation for a list of dictionaries and 'lines' to write each record on a separate line in the JSON file.

Output:

After running this program, you'll get a JSON file named 'data.json' containing the converted data in JSON format, just like in Method 1.

Conclusion:

Converting CSV to JSON is a common data transformation task that's essential for data analysis and manipulation. In this blog, we have explored multiple methods for achieving this in Python, from the straightforward use of the csv and json libraries to the power of Pandas, and the flexibility of creating custom methods.

By using the built-in csv and json libraries, you can easily convert CSV data to JSON, which is suitable for simple scenarios. However, if your data is more complex or you require advanced data manipulation capabilities, Pandas offers a comprehensive solution.

Comments (0)

There are no comments. Be the first to comment!!!