Convert JSON to TEXT File using Python – Data to Fish (2024)

The following template can be used to convert a JSON file 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)

Steps to Convert JSON to TEXT using Python

Step 1: Prepare the JSON

Let’s review a simple example, where we’ll create a JSON based on the data below:

ProductPrice
Desktop Computer700
Tablet250
Printer100
Laptop1200

This is how the JSON data would look like:

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

Step 2: Create the JSON file

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

For example, if you’re using Windows, then you may copy the JSON data into a Notepad.

Then, save the notepad with your desired file name and add the “.json file extension at the end of the file name.

For example, let’s save the JSON file as Product_List.json

Step 3: Install the Pandas Package

If you haven’t already done so, install the Pandas package using this command:

pip install pandas

Step 4: Convert the JSON file to a TEXT file using Python

For the final step, you may use the following template to convert the JSON file 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 example, let’s convert the JSON file “Product_List.json” to a text file “New_Products.txt” (note that 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 (adjusted to your paths), you’ll get the new text file at your specified location.

And if you open the text file, you’ll see the same data as captured in step-1:

Product,Price
Desktop Computer,700
Tablet,250
Printer,100
Laptop,1200

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

Convert JSON to TEXT File using Python – Data to Fish (2024)

FAQs

How to convert JSON to TXT file in Python? ›

How to Convert JSON to TXT via Python
  1. Install 'Aspose. Cells for Python via Java'.
  2. Add a library reference (import the library) to your Python project.
  3. Load JSON file with an instance of Workbook.
  4. Convert JSON to TXT by calling Workbook. save method.
  5. Get the conversion result of JSON to TXT.

How to convert JSON object to text in Python? ›

Steps to Convert JSON to TEXT using Python
  1. Step 1: Prepare the JSON. Let's review a simple example, where we'll create a JSON based on the data below: ...
  2. Step 2: Create the JSON file. ...
  3. Step 3: Install the Pandas Package. ...
  4. Step 4: Convert the JSON file to a TEXT file using Python.

How to convert JSON to readable format in Python? ›

We can use the dumps() method to get the pretty formatted JSON string.
  1. Python Pretty Print JSON String. import json json_data = '[{"ID":10,"Name":"Pankaj","Role":"CEO"},' \ '{"ID":20,"Name":"David Lee","Role":"Editor"}]' json_object = json. ...
  2. Python Pretty Print JSON File.

How to convert JSON data to text? ›

You can convert JSON to TXT with MConverter in three easy steps:
  1. Choose JSON files from your device. At the top of this page, drag and drop your JSONs. ...
  2. Click or tap on TXT from the list of target formats. ...
  3. Download your TXT files, after MConverter has finished processing them.

How do I extract text from a JSON file in Python? ›

Open the JSON file in read-only mode using the Python with() function. Load the JSON data into a variable using the Python load() function. Now, get the value of keys in a variable. Now convert the value of the dictionary into a list and slice the string using the split function.

How to write JSON data in a text file? ›

To write JSON data to a file, you need to follow a few steps. First, you need to open a file in write mode, specifying the file path. Then, you can use the json. dump() function to serialize the data and write it to the file.

How to write JSON data to a file in Python? ›

JSON can also be written to a file using the json. dump() method. Without first converting the dictionary into a JSON object, the "dump" function of the JSON package simply writes the dictionary to a file in the JSON format.

How to parse JSON text in Python? ›

If you have a JSON string, you can parse it by using the json. loads() method. The result will be a Python dictionary.

How do you convert data in JSON in Python? ›

Use the json.loads() function

you can turn it into JSON in Python using the json.loads() function. The json.loads() function accepts as input a valid string and converts it to a Python dictionary. This process is called deserialization – the act of converting a string to an object.

How to read a JSON file and convert to string in Python? ›

Load the JSON file into a file object and read its contents with the file. read() function returns a string containing the file's contents. Use the json. loads() function to convert this string object into the required Python dictionary and store the result in a variable jsonData.

How do you print the contents of a JSON file in Python? ›

To pretty print JSON in Python, you can use the json. dumps() function with the indent parameter, such as json. dumps(data, indent=4) . This function takes your JSON data and formats it in a more readable way.

How do I make a JSON file readable? ›

Format JSON is the same as beautify JSON : you make your JSON file readable by styling it with white spacing, newlines, and indentation. In short: paste your JSON file, then click the "Format" button in code mode, or select "Copy formatted" from the menu. This is how you make your JSON pretty.

How do you convert JSON data to string? ›

How to Convert JSON to String?
  1. Copy the entire text you want to convert from your JSON file.
  2. Once you are done with it, paste your content in the text box available on the website.
  3. Click on the option “String” in order to convert your text from the JSON file to String.

How to get data from a JSON file in Python? ›

Read JSON file in Python
  1. Import json module.
  2. Open the file using the name of the json file witn open() function.
  3. Open the file using the name of the json file witn open() function.
  4. Read the json file using load() and put the json data into a variable.
Aug 23, 2023

How to read JSON file as text? ›

Secondly, we read JSON String stored in a file using json.loads() for that we first convert the JSON file into a string using the file handling same as in the above example and then convert it into the string using read() function and rest of the procedure is same as we follow before using json.loads() method.

How do you export JSON to a file in Python? ›

JSON can also be written to a file using the json. dump() method. Without first converting the dictionary into a JSON object, the "dump" function of the JSON package simply writes the dictionary to a file in the JSON format.

How to convert JSON from string in Python? ›

Use the json.loads() function

you can turn it into JSON in Python using the json.loads() function. The json.loads() function accepts as input a valid string and converts it to a Python dictionary. This process is called deserialization – the act of converting a string to an object.

How to make a JSON file to readable format? ›

Format JSON is the same as beautify JSON : you make your JSON file readable by styling it with white spacing, newlines, and indentation. In short: paste your JSON file, then click the "Format" button in code mode, or select "Copy formatted" from the menu. This is how you make your JSON pretty.

How to convert txt file in Python? ›

Convert Text File to CSV using Python
  1. Step 1: Install the Pandas package. If you haven't already done so, install the Pandas package using this command: ...
  2. Step 2: Capture the path where your text file is stored. ...
  3. Step 3: Specify the path where the new CSV file will be saved. ...
  4. Step 4: Convert the text file to CSV using Python.
6 days ago

Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 6287

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.