Python JSON Parsing Using json.load() and loads()

In Python, the “JavaScript Object Notation” also known as “JSON” is parsed using the built-in json library. JSON parsing is a process of converting “JavaScript Object Notation” data into Python objects. For instance, to convert the JSON data into a Python dictionary or list, etc. the “json.load()” and “json.loads()” functions are used in Python.

In this blog post, the json.load() and json.loads() function are explained using numerous examples. The following contents will be discussed in this post:

How to Parse Data Using json.load() Function in Python?

The JSON module’s function named “json.load()” is used to parse the data in Python. The syntax of the “json.load()” function is shown below:

json.load(file_object)

The above syntax shows that the “json.load()” function takes the file object and retrieves the json object. 

Let’s see different examples of the json.load() function to parse json data.

Example 1: Parsing Data Using json.load()

In the below code the “json.load()” function is used to parse the JSON object into a Python object. 

The above snippet shows the content of a sample JSON file named “example.json”.

Code:

import json

with open("example.json", "r") as f:
    data = json.load(f)
   
    for key, value in data.items():
        print(key, ":", value)
  • The “json” module is imported at the beginning.
  • The json file called “example.json” is opened by utilizing the “with open()” function.
  • The “json.load()” function accepts the JSON file and returns the Python dictionary data.
  • The “for” loop iterates over the returned dictionary and shows the JSON data in key-value pairs.

Output:

The JSON data has been represented as a Dictionary in the above snippet.

Example 2: Accessing JSON Data

The following code is used to access the JSON data using the name of the key:

Code:

import json

with open("example.json", "r") as f:
    data = json.load(f)
    print(data["skills"])
    print(data["company"])

The parsed JSON data is accessed using the JSON key name enclosed within the square bracket along with the dictionary variable such as “data[“skills”]”.

Output:

The above output shows the specific data from the Python JSON file.

How to Parse Data Using json.loads() Function in Python?

The “json.loads()” function converts the “JSON” strings format data into a Python dictionary. The function is typically used to deserialize native strings, byte arrays, or “byte arrays with JSON data” into Python Dictionary objects.

The syntax of the “json.loads()” function is shown below:

json.loads(s)

From the above syntax, the “json.loads()” function takes the “JSON” string data and returns the dictionary. 

Let’s see different examples of json.loads() function to parse json data.

Example 1: Parsing Data Using json.loads()

In the below code the “json.loads()” function is used to parse the json data into Python object:

Code:

import json
data = '{"name": "Joseph", "age": "23", "Height": "5.7"}'
output = json.loads(data)
print(output)
print(type(output))
  • The JSON data in string format is initialized in the program and assigned to a variable named “output”.
  • The “json.loads()” accepts the JSON data and returns the dictionary object.

Output:

The above output shows the dictionary data of the given JSON strings.

Example 2: Accessing JSON Data

The following code is used to access the specific data from the given JSON data:

Code:

import json
json_data = '{"name": "Joseph", "age": "23", "Height": "5.7"}'
dict_data = json.loads(json_data)
print(dict_data)
print(dict_data['name'])
print(dict_data['Height'])

The key names of the JSON data are used inside the square brackets to access the specified key’s data such as “dict_data[‘name’]”.

Output:

The above output shows the specific data accessed using the key names.

Conclusion

The “json.load()” and “json.loads()” functions are used to parse JSON data into Python objects such as dictionaries, lists, etc. The specific data of JSON can also be accessed using the key name after converting it into the dictionary. The “json.load()” takes the URL or JSON file object and parses the given JSON data into Python. This guide presented a detailed guide on how to parse JSON data using the “json.load()” and “json.loads()” functions.

var authorName = "' . esc_js($post_author) . '";'; ?>