How to Parse JSON in Python
JSON
The JSON, the Java Script Object Notation, is an open standard file designed for data interchange; it is light-weighted text. The JSON uses human-readable text to store and transmit the data objects; it contains the variables and arrays. The JSON is independent of any programming language, and the JSON has a file extension of .json. The JSON is mainly used for the transmission of the data between a web application and a server; it acts the same as the transmitter in a communication device; both are mainly used for the transmission of the data. DouglasCrockford developed the JSON in 2001. The JSON has been derived from the javascript.
The JSON mainly consists of the two built-in functions or built-in methods.
- JSON.parse( )- It is mainly used to convert the JSON standard file strings into the objects of the JavaScript programming language.
- JSON.stringify( )- It is primarily used to convert an object in JavaScript into a JSON string.
JSON Python
Python programming language supports the JSON. The JSON provides the module that helps convert theJSON data into the python objects, such as dictionaries and strings. The data in the dictionary is stored in the key-value pair, and the data in the JSON is generally stored in the form of strings. Before parsing the data into the python dictionary, it is stored in the form of the string variable.
JSON String into Python Object
The data in the JSON is mainly stored in the form of strings; it can be parsed into the python dictionary. This operation can be performed by importing the JSON module. The JSON module consists of the two built-in functions or built-in methods to import the data- load and loads. To import a string, we generally use the loads module, which is read as 'load-s', that is, loading the string, whereas the load module is used when the data is in the form of bytes. The Boolean values in the JSON are read as true and false, which are converted into the python equivalent Boolean value 'True' and 'False'.
Example:
Let us consider an example of the name of the company and the total number of products produced by the company as the JSON data.
JSON data:
{
“company”: ‘Tata’,
“Production”: 2514522,
}
Code:
#importing the JSON module
importJSON
#JSON string
motors = ‘{“company”: “Tata”, “Production”: 2514522}’
#displaying the type of the company before converting it to a python dictionary
print(type(company))
#Python dictionary
company_dict =json.loads(company)
#displaying the type of the company after converting it into the python dictionary.
print(type(company_dict))
#displaying the company
print(company_dict[‘company’])
Output:
<class ‘str’>
<class ‘dict’>
Tata
Here, we can observe that first, we need to import the JSON module before performing any operation before parsing the data into the python dictionary; the data in the JSON is first stored in the form of the JSON string. Then we used the inbuilt function of the JSON module JSON.loads(); we will then parse it into the python dictionary or python list depending upon the input string; that is, if the input string contains only one data as input, then it will be parsed as the list, and if the data contains more than one input it will be parsed as a dictionary.
Convert JSON File into Python Object
This process is similar to parsing the JSON data stored in the strings. In this method, we first read the JSON files and then parse the JSON data into the python data. Here we require the open( ) function to read the data in the JSON file, and we use the load( ) built-in function. Here we use the .jsonto store the file.
File:
Students_marks.json
{
“name” : “Rohit”
“Marks” : 97
“Subject” : Mathematics
}
Code:
# importing the JSON module
importJSON
#opening the file
with open(‘Students_marks.json) as f:
data = json.load(f)
#displaying the data type
print(type(data))
#displaying the name of the student
print(data[‘name’])
Output:
<class ‘dict’>
Rohit
Here, we can observe that first, we need to import the JSON module before performing any operation; then, with the help of the open( ) function, we need to read the file before parsing the data into the python dictionary the data in the JSON is first stored in the form of the JSON string. Then we used the inbuilt function of the JSON module JSON.load(); it will then be parsed into the python dictionary or python list depending upon the input string; that is, if the input string contains only one data as input, then it will be parsed as the list, and if the data contains more than one input it will be parsed as a dictionary.