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
Convert CSV to JSON using Python Converting CSV to JSON using Python involves reading the CSV file, converting each row into a dictionary and then saving the data as a JSON file. For example, a CSV file containing data like names, ages and cities can be easily transformed into a structured JSON array, where each record is represent
2 min read
Convert JSON to GeoJSON Python GeoJSON has become a widely used format for representing geographic data in a JSON-like structure. If you have data in a standard JSON format and need to convert it into GeoJSON for mapping or analysis, Python provides several methods to make this conversion seamless. In this article, we will explor
4 min read
Convert JSON to string - Python Data is transmitted across platforms using API calls. Data is mostly retrieved in JSON format. We can convert the obtained JSON data into String data for the ease of storing and working with it. Python provides built-in support for working with JSON through the json module. We can convert JSON data
2 min read
Convert JSON to PNG in Python We are given JSON data and our task is to convert JSON to PNG in Python using different approaches. In this article, we will explore how to convert JSON data into PNG images using Python. Convert JSON to PNG in PythonBelow are some of the ways by which we can convert JSON to PNG in Python: Using pil
3 min read
Convert Bytes To Json using Python When dealing with complex byte data in Python, converting it to JSON format is a common task. In this article, we will explore different approaches, each demonstrating how to handle complex byte input and showcasing the resulting JSON output. Convert Bytes To JSON in PythonBelow are some of the ways
2 min read