How to Convert a JSON String to CSV using Python

In this guide, I’ll show you the steps to convert a JSON string to CSV using Python.

To begin, you may use the following template to perform the conversion:

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

In the next section, I’ll show you how to apply the above template in practice. I’ll also share the code to create a simple tool to convert your JSON string to CSV:

How to Convert a JSON String to CSV using Python

Steps to Convert a JSON String to CSV using Python

Step 1: Prepare the JSON String

To start, prepare the JSON string that you’d like to convert to CSV.

For example, let’s say that you’d like to prepare a JSON string based on the following information about different products:

ProductPrice
Desktop Computer700
Tablet250
iPhone800
Laptop1200

This is how the JSON string would look like for our example:

{"Product":{"0":"Desktop Computer","1":"Tablet","2":"iPhone","3":"Laptop"},"Price":{"0":700,"1":250,"2":800,"3":1200}}

Step 2: Create the JSON File

Once you have your JSON string ready, save it within a JSON file.

Alternatively, you may copy the JSON string into Notepad, and then save that file with a .json extension.

For example, open Notepad, and then copy the JSON string into it:

Notepad

Then, save the notepad with your desired file name and add the .json extension at the end of the file name. Here, I named the file as Product_List.json:

Save file

Step 3: Install the Pandas Package

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

pip install pandas

Step 4: Convert the JSON String to CSV using Python

You may now use the following template to assit you in converting the JSON string to CSV using Python:

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

For our example:

  • The path where I saved the JSON file is: C:\Users\Ron\Desktop\Test\Product_List.json
    • Where ‘Product_List’ is the file name, and ‘json’ is the file extension
  • The path where I’ll store the new CSV file is: C:\Users\Ron\Desktop\Test\New_Products.csv
    • Where ‘New_Products’ is the new file name, and ‘csv’ is the file extension

Here is the complete Python code to perform the conversion to CSV (you’ll need to adjust the paths to reflect the location where the files will be stored on your computer):

import pandas as pd
df = pd.read_json (r'C:\Users\Ron\Desktop\Test\Product_List.json')
export_csv = df.to_csv (r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None, header=True)

Run the code (adjusted to your paths) and you’ll see the new CSV file at your specified location:

How to Convert a JSON String to CSV using Python

Once you open the file, you’ll get the data about the Products:

Example of data

Tool to Convert a JSON String to CSV

Lastly, you may use the code below to convert your JSON string to CSV.

This code is based on the tkinter package, which can be used to build a Graphical user Interface (GUI) in Python:

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 getJSON ():
    global read_file
    
    import_file_path = filedialog.askopenfilename()
    read_file = pd.read_json (import_file_path)
    
browseButton_JSON = tk.Button(text="      Import JSON File     ", command=getJSON, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 130, window=browseButton_JSON)

def convertToCSV ():
    global read_file
    
    export_file_path = filedialog.asksaveasfilename(defaultextension='.csv')
    read_file.to_csv (export_file_path, index = None, header=True)

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

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()

Run the code in Python, and you’ll see this display:

How to Convert a JSON String to CSV using Python

Click on the ‘Import JSON File’ button:

JSON String to CSV using Python

Locate the JSON file. In our case, locate the Product_List file, and then click on Open:

Open file

Press on the ‘Convert JSON to CSV‘ button:

Convert a JSON String to CSV using Python

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

JSON String to CSV using Python

Your new CSV file will now get created:

New file

You may want to check the following guides for other types of conversions: