The following template can be used to convert a JSON string to a Text file 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 TEXT file will be stored\New File Name.txt', index = False)
Next, you’ll see the steps to apply the above template in practice, as well as obtain the syntax to create this file conversion tool:
Steps to Convert JSON String to TEXT File using Python
Step 1: Prepare the JSON string
Let’s review a simple example, where we’ll create a JSON string based on the data below:
Product | Price |
Desktop Computer | 700 |
Tablet | 250 |
iPhone | 800 |
Laptop | 1200 |
This is how the JSON string would look like:
{"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.
For example, if you’re using Windows, then you may copy the JSON string into Notepad:
Next, save the notepad with your desired file name and add the .json extension at the end of the file name. Here, the file was named as Product_List.json:
Step 3: Install the Pandas Package
If you haven’t already done so, install the Pandas package using this command (in Windows):
pip install pandas
Step 4: Convert the JSON String to TEXT using Python
For the final step, you may use the following template to convert the JSON string to a Text file 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 TEXT file will be stored\New File Name.txt', index = False)
For our example:
- The path where the JSON file was saved 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 the new Text file will be stored is: C:\Users\Ron\Desktop\Test\New_Products.txt
Where ‘New_Products‘ is the new file name, and ‘txt‘ is the file extension
Here is the complete code to convert the JSON string to a Text file (you’ll need to modify 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') df.to_csv (r'C:\Users\Ron\Desktop\Test\New_Products.txt', index = False)
Once you run the code in Python, you’ll get the new Text file:
And if you open the Text file, you’ll see the same data as captured in step-1:
Tool to Convert a JSON String to TEXT
The code below can be used to convert your JSON string to a Text file.
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 convertToTXT (): global read_file export_file_path = filedialog.asksaveasfilename(defaultextension='.txt') read_file.to_csv (export_file_path, index = False) saveAsButton_TXT = tk.Button(text='Convert JSON to TXT', command=convertToTXT, bg='green', fg='white', font=('helvetica', 12, 'bold')) canvas1.create_window(150, 180, window=saveAsButton_TXT) 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 in Python, you’ll see the following display:
Click on the ‘Import JSON File‘ button:
Locate your JSON file. In our case, locate the Product_List file, and then click on Open:
Press on the ‘Convert JSON to TXT‘ button:
Finally, type a name for your new Text file (e.g., New_Products) and then click on Save:
You’ll now see your new Text file:
You may also want to check the following guides for other types of conversions: