How to Convert CSV to JSON String using Python

You may use the following template in order to convert CSV to a JSON string using Python:

import pandas as pd
df = pd.read_csv (r'Path where the CSV file is saved\File Name.csv')
df.to_json (r'Path where the new JSON file will be stored\New File Name.json')

Next, I’ll review the steps to apply the above template in practice. I’ll also share the code to create a simple tool to convert your CSV file to a JSON string:

How to Convert CSV to JSON String using Python

Steps to Convert CSV to JSON String using Python

Step 1: Prepare the CSV File

Prepare the CSV file that you’d like to convert to a JSON string.

For example, I prepared a CSV file (called ‘Products‘) that contains the following information:

ProductPrice
Desktop Computer700
Tablet250
iPhone800
Laptop1200

Step 2: Install the Pandas Package

If you haven’t already done so, install the pandas package. You may use the following syntax to install the pandas package under Windows:

pip install pandas

Step 3: Convert the CSV to JSON String using Python

You may now use the following template to assist you in the conversion of the CSV file to a JSON string:

import pandas as pd
df = pd.read_csv (r'Path where the CSV file is saved\File Name.csv')
df.to_json (r'Path where the new JSON file will be stored\New File Name.json')

For our example:

  • The path where I saved the CSV file is: C:\Users\Ron\Desktop\Test\Products.csv
    • Where ‘Products‘ is the file name, and ‘csv‘ is the file extension
  • The path where the new JSON file will be stored is: C:\Users\Ron\Desktop\Test\New_Products.json
    • Where ‘New_Products‘ is the new file name, and ‘json‘ is the file extension

Here it the complete Python code to convert the CSV file to the JSON string (you’ll need to modify the paths to the location where the files will be stored on your computer):

import pandas as pd
df = pd.read_csv (r'C:\Users\Ron\Desktop\Test\Products.csv')
df.to_json (r'C:\Users\Ron\Desktop\Test\New_Products.json')

Run the code in Python (adjusted to your paths), and the new JSON file will get created at your specified location:

Files in a folder

Open the JSON file, and you’ll see the following string:

JSON String

Tool to Convert CSV to JSON String

For the final section of this guide, I’ll share the code to create a simple tool to convert your CSV file to JSON string.

The tool is based on the tkinter package that can be used to build a Graphical User Interface (GUI) in Python. Here is the full code:

import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import pandas as pd

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'lightsteelblue2', relief = 'raised')
canvas1.pack()

label1 = tk.Label(root, text='File Conversion Tool', bg = 'lightsteelblue2')
label1.config(font=('helvetica', 20))
canvas1.create_window(150, 60, window=label1)

def getCSV ():
    global read_file
    
    import_file_path = filedialog.askopenfilename()
    read_file = pd.read_csv (import_file_path)
    
browseButton_CSV = tk.Button(text="      Import CSV File     ", command=getCSV, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 130, window=browseButton_CSV)

def convertToJSON ():
    global read_file
    
    export_file_path = filedialog.asksaveasfilename(defaultextension='.json')
    read_file.to_json (export_file_path)

saveAsButton_JSON = tk.Button(text='Convert CSV to JSON', command=convertToJSON, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 180, window=saveAsButton_JSON)

def exitApplication():
    MsgBox = tk.messagebox.askquestion ('Exit Application','Are you sure you want to exit the application',icon = 'warning')
    if MsgBox == 'yes':
       root.destroy()
     
exitButton = tk.Button (root, text='       Exit Application     ',command=exitApplication, bg='brown', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 230, window=exitButton)

root.mainloop()

Once you run the code, you’ll see this display:

How to Convert CSV to JSON String using Python

Click on the ‘Import CSV File‘ button:

CSV to JSON String using Python

Locate your CSV file. In our example, locate the ‘Products’ file, and then click on Open:

Locate file

Next, click on the ‘Convert CSV to JSON‘ button:

Convert CSV to JSON String using Python

Finally, type a name for the new JSON file (e.g., ‘New_Products‘), and then click on Save:

Save File

Your new JSON file will now get created:

How to Convert CSV to JSON String using Python

You may also want to check the following guides that explain how to convert: