JSON stands for JavaScript Object Notation and it is the most common and widely used data structure format to exchange data, between server and client. From rest
 
  APIs
 
 to
 
  NoSQL databases
 
 JSON is used everywhere. And luckily python comes with an inbuilt module
 
  json
 
 where
 
  
   Python parses JSON data
  
 
 . Here in this Python tutorial we will explore the Python
 
  json
 
 module and learn How can Python parse JSON and write Python code to
 
  
   parse JSON to Dictionary
  
 
 .
What is JSON?
JavaScript Object Notation (JSON) is a data structure format that is derived from the JavaScript Programming language. The data representation of JSON looks similar to the python dictionary . And JSON format is used to send data from a server to a web-page.
JSON Example
 {
    "username": "Sam",
    "age": 35,
    "cart": ["shoes", "tshirt", "iphone"],
    "orders": [
        {
            "year": 2019,
            "total orders": 30,
            "spent": 6000
        },
        {
            "year": 2020,
            "total orders": 43,
            "spent":7000
        }
    ]
}
The above example is the data representation of JSON, and here you can see that the structure of JSON is similar to the Python dictionary. And it also supports string, number, list, and nested objects.
Python Serialization and Deserialization
As we know that JSON is the common data format for sending and receiving data between server and client. So, if we want to send data to a rest API or server using Python, we first need to encode our data to JSON format. And encoding of Python data to JSON data is known as Serialization in python. And if we want to decode JSON data to the python data, then we deserialize it, and that process is known as Deserialization or Python parse JSON. And in Deserialization the Python Parse JSON to dictionary data structure, because both JSON and Python dictionary has similar structure format.
< Note >: serialization and deserialization are the processes for type conversion between Python to JSON and JSON to Python dictionary.
Serialization type conversion
During serialization, we convert the Python data object to the corresponding JSON or JavaScript data type
| Python | JSON | 
| dict | object | 
| list, tuple | array | 
| str | string | 
| int, long, float | number | 
| True | true | 
| False | false | 
| None | null | 
Deserialization Type Conversion
In Deserialization we parse JSON data value to corresponding Python data object or types
| JSON | Python | 
| object | dict | 
| array | list | 
| string | str | 
| integer number | int | 
| real number | float | 
| true | True | 
| false | False | 
| null | None | 
Python JSON module
 Python comes with an inbuilt module called
 
  json
 
 which can serialize and deserialize python object to JSON data and JSON data to python object respectively. The python
 
  json
 
 module makes it easy for the developer to deal with the JSON data.
import python JSON module
 To use
 
  json
 
 module in a python program we first need to import it using the python
 
  import
 
 keyword
 
  import json
 
Python Parse JSON (deserialization)
 There are two
 
  json
 
 methods
 
  json.loads()
 
 and
 
  json.load()
 
 to parse JSON string and JSON file in Python.
Parse JSON string in Python
 Most of the time we receive JSON object in string format, so using the python
 
  json.loads()
 
 method we can convert string Python JSON to dict.
Example
import json
user_json_data ='''{
    "username": "Sam",
    "age": 35,
    "cart": ["shoes", "tshirt", "iphone"],
    "orders": [
        {
            "year": 2019, "total orders": 30,  "spent": 6000
        },
        { "year": 2020, "total orders": 43 ,"spent":7000
        }
    ]
}
'''
user_python_dict = json.loads(user_json_data )
print(user_python_dict)
print(user_python_dict["orders"])
Output
{'username': 'Sam', 'age': 35, 'cart': ['shoes', 'tshirt', 'iphone'], 'orders': [{'year': 2019, 'total orders': 30, 'spent': 6000}, {'year': 2020, 'total orders': 43, 'spent': 7000}]}
[{'year': 2019, 'total orders': 30, 'spent': 6000}, {'year': 2020, 'total orders': 43, 'spent': 7000}]
Behind the code
 In the above example, you can see that we converted the
 
  user_json_data
 
 JSON string to a Python dictionary object
 
  user_python_dict.
 
 Here if we used the python
 
  dict()
 
 method instead of
 
  json.loads()
 
 we would receive an error.
Parse JSON file in Python
 First, using the
 
  Python file handling technique
 
 Python load JSON file, then using
 
  json.load()
 
 method Python parses the JSON data.
#user.json
{
    "username": "Sam",
    "age": 35,
    "cart": ["shoes", "tshirt", "iphone"],
    "orders": [
        {
            "year": 2019,
            "total orders": 30,
            "spent": 6000
        },
        {
            "year": 2020,
            "total orders": 43,
            "spent":7000
        }
    ]
}
#python
import json
with open ("user.json", 'r') as f:
    user_python_dict = json.load(f)
print(user_python_dict)
print(user_python_dict["orders"])
Output
{'username': 'Sam', 'age': 35, 'cart': ['shoes', 'tshirt', 'iphone'], 'orders': [{'year': 2019, 'total orders': 30, 'spent': 6000}, {'year': 2020, 'total orders': 43, 'spent': 7000}]}
[{'year': 2019, 'total orders': 30, 'spent': 6000}, {'year': 2020, 'total orders': 43, 'spent': 7000}]
 
  Note:
 
 For JSON string we use
 
  json.loads()
 
 method to parse the JSON data And for JSON file we use the
 
  json.load()
 
 method.
Python convert to JSON string (serialization)
 To convert a Python dictionary object to JSON string we use the Python
 
  json.dumps()
 
 method. As the python dictionary is very similar to the JSON object so the
 
  json.dumps()
 
 method accepts a Python dictionary object.
Example convert python dictionary to JSON
import json
user_dict = {"name":"Rahul", "age":23, "adult": True}
json_string = json.dumps(user_dict)
print(json_string)
Output
{"name": "Rahul", "age": 23, "adult": true}
Create a JSON file and write JSON data Python
 With the help of Python file handing, we can create a JSON file and using the
 
  json.dump()
 
 method we can write JSON data in that JSON file.
Example
import json
user_dict = {"name":"Rahul", "age":23, "adult": True}
with open("user_data.json", "w") as json_file:
    json.dump(user_dict, json_file)
 The above program will create a
 
  user_data.json
 
 file and write the JSON data in that file.
Prettify the JSON data:
 For a large data set the JSON data could be clustered and hard to read, but with the help of
 
  indent
 
 and
 
  sort_keys
 
 parameters, we can prettify the data and make it more readable.
Example
import json
user_dict = {"user1":{"name":"Rahul", "age":23, "adult": True},"user2":{"name":"Ram", "age":20, "adult": True}}
print(json.dumps(user_dict, indent = 4, sort_keys=True))
Output
{
    "user1": {
        "adult": true,
        "age": 23,
        "name": "Rahul"
    },
    "user2": {
        "adult": true,
        "age": 20,
        "name": "Ram"
    }
}
| Python json methods | Description | 
| json.loads(python_json_string) | It parses Python string JSON to dict. (Python JSON to dict) | 
| json.load(json_file) | Using File handling Python load JSON file, and json.load() method parse the JSON file data to the dictionary. (Python JSON to dict) | 
| json.dumps(dict) | It converts the Python dictionary to the corresponding JSON string. (Dict to Python String JSON) | 
| json.dump(dict, json_file) | It writes JSON data in JSON file (Dict to Python string JSON) | 
Conclusion
 In this Python tutorial, we learned how can we use the Python
 
  json
 
 module to parse JSON in Python. The python
 
  json.loads(python_json_string)
 
 method parses the json string, whereas
 
  json.load(json_file)
 
 parse the json file. Similarly, the
 
  json.dumps(python_dict)
 
 method converts the python object to the corresponding json string and
 
  json.dump()
 
 method can write in the json file.
People are also reading:
- Google Page Ranking in Python
 - How to Extract All PDF Links in Python?
 - Analyze Sentiment using VADER in Python
 - Extract Images from PDF in Python
 - Python SIFT Feature Extraction
 - How to Create Plots with Plotly In Python?
 - Port Scanner in Python
 - How to Generate and Read QR Code in Python?
 - Extract Weather Data from Rapid API in Python
 - Screen Recorder in Python