📘 Introduction

JSON is one of the most widely used data formats for APIs, configurations, storage, and modern applications. Converting CSV to JSON in Python is incredibly simple using Pandas. In this tutorial, we will walk through the full process: creating a sample CSV file, loading it into a Pandas DataFrame, converting it to JSON, and verifying the output.

✅ Prerequisites

Before you begin, make sure you have:

🐍☑️ Installed Python
🌐☑️ Created and activated a virtual environment (venv)

📦1️⃣ Install Libraries

Install the following Python packages using pip:

pip install pandas

📁2️⃣ Create Sample CSV file

First, create a sample CSV file called student.csv within your project folder:

project-folder/
└── student.csv

Add the following content to the .csv file:

id,name,major
s1,Lara Fitzgerald,Data Analytics
s2,Mike Meyer,Data Engineering
s3,Eliza Gomez,Data Science
s4,Travis Robinson,Data Analytics
s5,Jackie Brown,Data Engineering
s6,Caleb Pearson,Data Science
s7,Ava Johansson,Data Science
s8,Nathan Williams,Data Engineering
s9,Cooper Harris,Data Analytics
s10,Murphy Fraser,Data Science

🐍3️⃣ Create Python script

In the same folder as the CSV file, create a Python file (.py) or Jupyter notebook (.ipynb):

project-folder/
├── convert_csv_to_json.ipynb
└── student.csv

📥4️⃣ Import Libraries

Open your Python file and start by importing the required Python modules:

import pandas as pd

⬆️5️⃣ Read CSV File

Read the CSV file into a Pandas DataFrame.

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In