Questions And Answers

More Tutorials

Python Json Module

JSON stands for JavaScript Object Notation. JSON is a data-interchange format derived from JavaScript. It is mostly used for storing or transferring data. So, if we want our program to interact with the internet, we must be familiar with this module, even only to send or receive data through the internet. It is one of Python's most important modules because if we want it to interact only a bit through the internet, the Json module must be imported first. A JSON is an unordered collection of key and value pairs similar to a python dictionary. The following are some important points about JSON.
1. Keys are unique strings that cannot be null.
2. Values can be anything from a String, Number, Tuple, Boolean, list, or null.
3. A JSON is represented by a string which is enclosed within curly braces { } with key-value pairs which are separated by a colon ( : ), and pairs separated by a comma(, ).

Below is an example of JSON data. We can notice that the data representation is similar to Python dictionaries.

{"name": "harry", "salary": 9000, "language": "Python"}

JSON in Python:

JSON is already built-in in Python, so no need for an installation command. We can import it so we may start working on it. JSON module in Python helps us in converting the data structures to JSON strings. Use the import function to import the JSON module in your Python program.

import json

If we convert a JSON string to Python, the resultant will be a dictionary. The conversion is also known as parsing, and that is the keyword we use professionally for the conversion. We can convert from JSON to Python or Python to JSON by using json.loads() or json.dumps() methods. Methods:

load(): This method is used to load data from a JSON file into a python dictionary.
Loads( ): This method is used to load data from a JSON variable into a python dictionary.
dump(): This method is used to load data from the python dictionary to the JSON file.
dumps(): This method is used to load data from the python dictionary to the JSON variable.

Following is the program showing the use of JSON package in Python

import json
a ={"name":"harry","salary":9000,"language":"Python"}  
# conversion to JSON done by dumps() function
b = json.dumps(a)
print(b) # printing the output


{"name": "harry", "salary": 9000, "language": "Python"}

What parsing actually does?

Parsing converts the code from one form to another, making it compatible with running on the other platform by changing all the little syntax differences and making it perfect for running on the other platform. The following table shows how Python objects are converted to JSON objects.


import json

data = '{"var1":"harry", "var2":56}'

parsed = json.loads(data)

#Task 1 - json.load?

data2 = {
    "channel_name": "CodeWithHarry",
    "cars": ['bmw', 'audi a8', 'ferrari'],
    "fridge": ('roti', 540),
    "isbad": False

jscomp = json.dumps(data2)


In this page (written and validated by ) you learned about Python Json Module . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Regular Expressions.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.