JSON in Python

JSON is an acronym for JavaScript Object Notation. Python has a built-in package named ‘json’ to support JSON in Python. JSON is basically used for encoding and decoding data. The process of encoding the JSON data is referred to as serialization as it involves converting data into a series of bytes that can be stored and transmitted between servers and web application. Since serialization is encoding of the data, we can guess the term used for decoding. Yes, it is deserialization.

Watch this Python Numpy Tutorial Video for Beginners:

Python JSON JSON in Python JSON is an acronym for JavaScript Object Notation. Python has a built-in package named ‘json’ to support JSON in Python. JSON is basically used for encoding and decoding data. The process of encoding the JSON data is referred to as serialization as it involves converting data into

In this module, we will learn about JSON in Python and cover the following topics:

So, without further delay, let’s get started.

Become a master of Python by going through this online Python Course in Toronto!

Parsing JSON in Python:

How to read json file in python?

Reading JSON data from a file is very easy.  json.load() method reads the string from a file, parses the JSON data. Then it populates a Python dictionary with the parsed data and returns it back to us.

Example:

#import json in Python
import json
with open('Intellipaat.txt') as json_file:


#load json file python
data = json.load(json_file)


for p in data['Course']:


print('Name: ' + p['name'])
print('Website: ' + p['website'])
print('From: ' + p['from'])
print('')

How to convert JSON String to Python Object?

If we have a JSON string or JSON data, we can easily parse it using the json.loads() method found in the json package. To make use of this method, we have to import the json package offered by Python. As discussed above, this method is called deserialization, as we are converting the JSON encoded data into Python objects. Deserialization takes place as per the following table.

For example, the JSON data of the object type will be converted into a Python dictionary.

JSONPython
objectdict
arraylist
stringstr
number (int)int
number (real)float
truetrue
falsefalse
nullnone

Example of Parsing Json in Python:

import json
intellipaat = '{"course":"python", "topic":"Python JSON"}'


#parse a JSON string using json.loads()
intellipaat_dict = json.loads(intellipaat)


print(intellipaat_dict)
print(type(intellipaat))
print(type(intellipaat_dict))


Output:
{'course': 'python', 'topic': 'Python JSON'}
<class 'str'>
<class 'dict'>

In the above example, intellipaat is a JSON string and intellipaat_dict is a Python dictionary.

Get certified from this top Python Course in Singapore today!

Converting from Python Dictionary to JSON

We can also convert Python data types into JSON format using the json.dumps() method. Let us take a look at the example and understand how to convert Python object to Json in Python programming.

Example:

import json
intellipaat = {"course":"python", "topic":"Python JSON"}
intellipaat_json = json.dumps(intellipaat)


print(intellipaat_json)
print(type(intellipaat))
print(type(intellipaat_json))


Output:
{"course": "python", "topic": "Python JSON"}<class 'dict'><class 'str'>

While converting Python objects into JSON, these objects get converted into the equivalent JSON type as per the following table:

PythonJSON
dictObject
listArray
tupleArray
strString
intNumber
floatNumber
Truetrue
Falsefalse
Nonenull

Formatting the Result of Json in Python

Even though we have learned how to convert the Python object into JSON data, it can still be very hard to read the converted JSON data, with no indentation and no line breaks. To make it more readable, there are various parameters of the json.dumps() method that we can use.

Example:

#using indent parameter to provide indentation
json.dumps(b, indent = 3)
We can also include various separators, such as, comma and colon.

Example:

json.dumps(b, indent = 3, separators = (“.”,”=”))

Learn end-to-end Python concepts through the Python Course in Hyderabad to take your career to a whole new level!

Ordering the Result of Json in Python

In certain cases where we might want to sort the resultant JSON data after converting it from Python to JSON, we can simply use another parameter of the json.dumps() method, the sort_keys.

With the help of this parameter, we can define if we want the result to be sorted or not, using values such as true and false.

Example:

json.dumps(b, indent = 3, sort_keys=True)

With this, we come to an end of this module in python Tutorial. You can also go through this blog on Python for Data Science if you want to know why python is the most preferred language for data science.

Further, check out our offers for Python training Course and also refer to the trending Python interview questions prepared by the industry experts.

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
19 + 7 =