Convert Excel To Json With Python
Last Updated :
28 Apr, 2025
In the realm of data manipulation and analysis, the ability to convert data between different formats is a valuable skill. Excel and JSON are two widely used formats, and Python, with its powerful libraries, provides a seamless way to convert Excel files into JSON. In this article, we will see how we can convert Excel to JSON in Python.
Excel File: Link
student.xlsx (sheet_name = suraj1)

Convert Excel to JSON with Python
Below are some of the ways by which we can convert excel to JSON in Python:
- Using to_json() Function
- Using openpyxl Module
- Using excel2json Module
Using to_json() Function
In this example, Python's pandas
library is utilized to read an Excel file named "student.xlsx" from the sheet named "suraj1." The data is then converted to JSON format using to_json()
method, and the resulting JSON data is printed to the console using print(json_data)
.
Python3
import pandas as pd
import json
# Convert Excel To Json With Python
data = pd.read_excel("student.xlsx", sheet_name="suraj1")
json_data = data.to_json()
# Print the JSON data
print(json_data)
Output:
[{"Country name": "Mauritania", "2023": 2023, "36th": "37th"}, {"Country name": "North Korea", "2023": 2023, "36th": "38th"}, {"Country name": "Angola", "2023": 2023, "36th": "39th"}, {"Country name": "Iran", "2023": 2023, "36th": "40th"}, {"Country name": "Bangladesh", "2023": 2023, "36th": "41st"}, {"Country name": "Equatorial Guinea", "2023": 2023, "36th": "42nd"}, {"Country name": "Malawi", "2023": 2023, "36th": "43rd"}, {"Country name": "Rwanda", "2023": 2023, "36th": "44th"}, {"Country name": "Comoros", "2023": 2023, "36th": "45th"}, {"Country name": "Djibouti", "2023": 2023, "36th": "46th"}, {"Country name": "Togo", "2023": 2023, "36th": "47th"}, {"Country name": "Zambia", "2023": 2023, "36th": "48th"}, {"Country name": "Mauritania", "2023": 2023, "36th": "36th"}, {"Country name": "North Korea", "2023": 2023, "36th": "37th"}, {"Country name": "Angola", "2023": 2023, "36th": "38th"}, {"Country name": "Iran", "2023": 2023, "36th": "39th"}]
Using openpyxl Module
In this example, the openpyxl
library is employed to load an Excel workbook named "student.xlsx," focusing on the sheet named "suraj1." The script iterates through the rows and columns, extracts data, and organizes it into a list of dictionaries. Finally, the extracted data is converted to JSON format using json.dumps()
and printed to the console with print(json_data)
.
Python3
from openpyxl import load_workbook
from json import dumps
# Load Excel workbook
wb = load_workbook("student.xlsx")
# Choose a specific sheet
sheet = wb["suraj1"]
# Find the number of rows and columns in the sheet
rows = sheet.max_row
columns = sheet.max_column
# List to store all rows as dictionaries
lst = []
# Iterate over rows and columns to extract data
for i in range(1, rows):
row = {}
for j in range(1, columns):
column_name = sheet.cell(row=1, column=j)
row_data = sheet.cell(row=i+1, column=j)
row.update(
{
column_name.value: row_data.value
}
)
lst.append(row)
# Convert extracted data into JSON format
json_data = dumps(lst)
# Print the JSON data
print(json_data)
Output:
[{"Country name": "Mauritania", "2023": 2023, "36th": "37th"}, {"Country name": "North Korea", "2023": 2023, "36th": "38th"}, {"Country name": "Angola", "2023": 2023, "36th": "39th"}, {"Country name": "Iran", "2023": 2023, "36th": "40th"}, {"Country name": "Bangladesh", "2023": 2023, "36th": "41st"}, {"Country name": "Equatorial Guinea", "2023": 2023, "36th": "42nd"}, {"Country name": "Malawi", "2023": 2023, "36th": "43rd"}, {"Country name": "Rwanda", "2023": 2023, "36th": "44th"}, {"Country name": "Comoros", "2023": 2023, "36th": "45th"}, {"Country name": "Djibouti", "2023": 2023, "36th": "46th"}, {"Country name": "Togo", "2023": 2023, "36th": "47th"}, {"Country name": "Zambia", "2023": 2023, "36th": "48th"}, {"Country name": "Mauritania", "2023": 2023, "36th": "36th"}, {"Country name": "North Korea", "2023": 2023, "36th": "37th"}, {"Country name": "Angola", "2023": 2023, "36th": "38th"}, {"Country name": "Iran", "2023": 2023, "36th": "39th"}]
Using excel2json Library
This is a Python library that converts an Excel file to JSON format. This is a simple way to convert an Excel file to JSON.
pip install excel2json
In this example, the excel2json
library is used to simplify the process of converting an Excel file ("student.xls") to JSON. The convert_from_file
function is invoked to perform the conversion, providing a convenient way to automate the task without manually handling the intricacies of Excel-to-JSON conversion.
Python3
from excel2json import convert_from_file
# Convert Excel file to JSON
convert_from_file("student.xls")
Output:
[{"Country name": "Mauritania", "2023": 2023, "36th": "37th"}, {"Country name": "North Korea", "2023": 2023, "36th": "38th"}, {"Country name": "Angola", "2023": 2023, "36th": "39th"}, {"Country name": "Iran", "2023": 2023, "36th": "40th"}, {"Country name": "Bangladesh", "2023": 2023, "36th": "41st"}, {"Country name": "Equatorial Guinea", "2023": 2023, "36th": "42nd"}, {"Country name": "Malawi", "2023": 2023, "36th": "43rd"}, {"Country name": "Rwanda", "2023": 2023, "36th": "44th"}, {"Country name": "Comoros", "2023": 2023, "36th": "45th"}, {"Country name": "Djibouti", "2023": 2023, "36th": "46th"}, {"Country name": "Togo", "2023": 2023, "36th": "47th"}, {"Country name": "Zambia", "2023": 2023, "36th": "48th"}, {"Country name": "Mauritania", "2023": 2023, "36th": "36th"}, {"Country name": "North Korea", "2023": 2023, "36th": "37th"}, {"Country name": "Angola", "2023": 2023, "36th": "38th"}, {"Country name": "Iran", "2023": 2023, "36th": "39th"}]
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read