bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Json in Python

JSON is an acronym for JavaScript Object Notation. Python has a built-in package named json to support JSON programming 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 the data, you can guess what term is used for decoding. Yes, it is deserialization.

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

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

Parse JSON – Converting from JSON to Python

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

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

JSON Python
object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None

Example:

import json
intellipaat = {“course”:” python”, “topic”: ” Python JSON”}
intellipaat_dict = json.loads(intellipaat)
print(intellipaat_dict)

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

Convert from Python to JSON

We can also convert Python data into Json format using dumps() method  as shown in the example below:

import json
a = {“course”:”Python”, “topic”:”Python JSON “}
b = json.dumps(a)
print(b)

while converting Python objects into JSON, the Python object gets converted into the equivalent JSON type as per the following table:

Python JSON
dict Object
list Array
tuple Array
str String
int Number
float Number
True true
False false
None null

Data science masters program

Format the result

Even though we have learned how to convert the Python object into JSON data, but 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 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 = (“.”,”=”))

Order the result

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 json.dumps() method which is, sort_keys.

Using this parameter, you can define if you 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, the next module highlights Python RegEx. See you there!

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on Python JSON"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Python JSON.