Open In App

Convert Text file to JSON in Python

Last Updated : 13 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

JSON (JavaScript Object Notation) is a data-interchange format that is human-readable text and is used to transmit data, especially between web applications and servers. The JSON files will be like nested dictionaries in Python. To convert a text file into JSON, there is a json module in Python. This module comes in-built with Python standard modules, so there is no need to install it externally. See the following table given below to see serializing JSON i.e. the process of encoding JSON.

Python object JSON object
dict object
list, tuple array
str string
int, long, float numbers
True true
False false
None null

To handle the data flow in a file, the JSON library in Python uses dump() function to convert the Python objects into their respective JSON object, so it makes easy to write data to files. Syntax:

json.dump()

Various parameters can be passed to this method. They help in improving the readability of the JSON file. They are :

  • dict object : the dictionary which holds the key-value pairs.
  • indent : the indentation suitable for readability(a numerical value).
  • separator : How the objects must be separated from each other, how a value must be separated from its key. The symbols like “, “, “:”, “;”, “.” are used
  • sort_keys : If set to true, then the keys are sorted in ascending order

Here the idea is to store the contents of the text as key-value pairs in the dictionary and then dump it into a JSON file. A simple example is explained below. The text file contains a single person’s details. The text1.txt file looks like: The text file-sample1.txt Now to convert this to JSON file the code below can be used: 

Python3




# Python program to convert text
# file to JSON
 
 
import json
 
 
# the file to be converted to
# json format
filename = 'data.txt'
 
# dictionary where the lines from
# text will be stored
dict1 = {}
 
# creating dictionary
with open(filename) as fh:
 
    for line in fh:
 
        # reads each line and trims of extra the spaces
        # and gives only the valid words
        command, description = line.strip().split(None, 1)
 
        dict1[command] = description.strip()
 
# creating json file
# the JSON file is named as test1
out_file = open("test1.json", "w")
json.dump(dict1, out_file, indent = 4, sort_keys = False)
out_file.close()


When the above code is executed, if a JSON file exists in the given name it is written to it, otherwise, a new file is created in the destination path and the contents are written to it. Output: The resultant JSON file: Note the below line of code:

command, description = line.strip().split(None, 1)

Here split(None, 1) is used to trim off all excess spaces between a key-value pair and ‘1’ denotes split only once in a line. This ensures in a key-value pair, the spaces in the value are not removed and those words are not split. Only the key is separated from its value. How to convert if multiple records are stored in the text file ? Let us consider the following text file which is an employee record containing 4 rows. sample2.txt The idea is to convert each employee’s detail into an intermediate dictionary and append that to one main resultant dictionary. For each intermediate dictionary a unique id is created and that serves as the key. Thus here the employee id and an intermediate dictionary make a key-value pair for the resultant dictionary to be dumped. 

Python3




# Python program to convert text
# file to JSON
 
 
import json
 
 
# the file to be converted
filename = 'data.txt'
 
# resultant dictionary
dict1 = {}
 
# fields in the sample file
fields =['name', 'designation', 'age', 'salary']
 
with open(filename) as fh:
     
 
     
    # count variable for employee id creation
    l = 1
     
    for line in fh:
         
        # reading line by line from the text file
        description = list( line.strip().split(None, 4))
         
        # for output see below
        print(description)
         
        # for automatic creation of id for each employee
        sno ='emp'+str(l)
     
        # loop variable
        i = 0
        # intermediate dictionary
        dict2 = {}
        while i<len(fields):
             
                # creating dictionary for each employee
                dict2[fields[i]]= description[i]
                i = i + 1
                 
        # appending the record of each employee to
        # the main dictionary
        dict1[sno]= dict2
        l = l + 1
 
 
# creating json file       
out_file = open("test2.json", "w")
json.dump(dict1, out_file, indent = 4)
out_file.close()


The attributes associated with each column is stored in a separate list called ‘fields’. In the above code, Each line is split on the basis of space and converted into a dictionary. Each time the line print(attributes) get executed, it appears as given below.

['Lisa', 'programmer', '34', '54000']
['Elis', 'Trainee', '24', '40000']
['Rickson', 'HR', '30', '47000']
['Kate', 'Manager', '54', '63000']

The JSON file created by this code looks like : test2.json



Previous Article
Next Article

Similar Reads

Convert Text and Text File to PDF using Python
PDFs are one of the most important and widely used digital media. PDF stands for Portable Document Format. It uses .pdf extension. It is used to present and exchange documents reliably, independent of software, hardware, or operating system. Converting a given text or a text file to PDF (Portable Document Format) is one of the basic requirements in
3 min read
Saving Text, JSON, and CSV to a File in Python
Python allows users to handle files (read, write, save and delete files and many more). Because of Python, it is very easy for us to save multiple file formats. Python has in-built functions to save multiple file formats. Opening a text file in Python Opening a file refers to getting the file ready either for reading or for writing. This can be don
5 min read
Python - Difference between json.dump() and json.dumps()
JSON is a lightweight data format for data interchange which can be easily read and written by humans, easily parsed and generated by machines. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called json. Note: For more information, refer to Working With JSON Data in Python json.dumps() json.
2 min read
Python - Difference Between json.load() and json.loads()
JSON (JavaScript Object Notation) is a script (executable) file which is made of text in a programming language, is used to store and transfer the data. It is a language-independent format and is very easy to understand since it is self-describing in nature. Python has a built-in package called json. In this article, we are going to see Json.load a
3 min read
Python: Convert Speech to text and text to Speech
Speech Recognition is an important feature in several applications used such as home automation, artificial intelligence, etc. This article aims to provide an introduction on how to make use of the SpeechRecognition and pyttsx3 library of Python.Installation required: Python Speech Recognition module: pip install speechrecognition PyAudio: Use the
3 min read
Convert Text Image to Hand Written Text Image using Python
In this article, we are going to see how to convert text images to handwritten text images using PyWhatkit, Pillow, and Tesseract in Python. Module needed: Pytesseract: Sometimes known as Python-tesseract, is a Python-based optical character recognition (OCR) program. It can read and recognize text in photos, license plates, and other documents. To
2 min read
Convert Text File to CSV using Python Pandas
Let's see how to Convert Text File to CSV using Python Pandas. Python will read data from a text file and will create a dataframe with rows equal to number of lines present in the text file and columns equal to the number of fields present in a single line. See below example for better understanding. Dataframe created from upper text file will look
2 min read
Convert PDF File Text to Audio Speech using Python
Let us see how to read a PDF that is converting a textual PDF file into audio. Packages Used: pyttsx3: It is a Python library for Text to Speech. It has many functions which will help the machine to communicate with us. It will help the machine to speak to usPyPDF2: It will help to the text from the PDF. A Pure-Python library built as a PDF toolkit
2 min read
Create a GUI to convert CSV file into excel file using Python
Prerequisites: Python GUI – tkinter, Read csv using pandas CSV file is a Comma Separated Value file that uses a comma to separate values. It is basically used for exchanging data between different applications. In this, individual rows are separated by a newline. Fields of data in each row are delimited with a comma. Modules Needed Pandas: Python i
3 min read
How to convert PDF file to Excel file using Python?
In this article, we will see how to convert a PDF to Excel or CSV File Using Python. It can be done with various methods, here are we are going to use some methods. Method 1: Using pdftables_api Here will use the pdftables_api Module for converting the PDF file into any other format. It's a simple web-based API, so can be called from any programmin
2 min read
three90RightbarBannerImg