Python JSON load() and loads() for JSON Parsing
Excerpt
Understand use of json.loads() and load() to parse JSON. Read JSON encoded data from a file or string and convert it into Python dict
This article demonstrates how to use Python’s json.load()
and json.loads()
methods to read JSON data from file and String. Using the json.load()
and json.loads()
method, you can turn JSON encoded/formatted data into Python Types this process is known as JSON decoding. Python built-in module json provides the following two methods to decode JSON data.
To parse JSON from URL or file, use json.load()
. For parse string with JSON content, use json.loads()
.
Python JSON parsing using load and loads
Syntax of the json.load()
and json.loads()
We can do many JSON parsing operations using the load
and loads()
method. First, understand it’s syntax and arguments, then we move to its usage one-by-one.
Synatx of json.load()
Syntax of json.loads()
All arguments have the same meaning in both methods.
Parameter used:
The json.load()
is used to read the JSON document from file and The json.loads()
is used to convert the JSON String document into the Python dictionary.
**fp**
file pointer used to read a text file, binary file or a JSON file that contains a JSON document.**object_hook**
is the optional function that will be called with the result of any object literal decoded. The Python built-in json module can only handle primitives types that have a direct JSON equivalent (e.g., dictionary, lists, strings, Numbers, None, etc.). But when you want to convert JSON data into a custom Python type, we need to implement custom decoder and pass it as an objectobject_hook
to aload()
method so we can get a custom Python type in return instead of a dictionary.**object_pairs_hook**
is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value ofobject_pairs_hook
will be used instead of the Python dictionary. This feature can also be used to implement custom decoders. Ifobject_hook
is also defined, theobject_pairs_hook
takes priority.parse_float
is optional parameters but, if specified, will be called with the string of every JSON float and integer to be decoded. By default, this is equivalent tofloat(num_str)
.parse_int
if specified, it will be called with the string of every JSON int to be decoded. By default, this is equivalent toint(num_str)
.
We will see the use of all these parameters in detail.
json.load()
to read JSON data from a file and convert it into a dictionary
Using a json.load()
method, we can read JSON data from text, JSON, or binary file. The json.load()
method returns data in the form of a Python dictionary. Later we use this dictionary to access and manipulate data in our application or system.
Mapping between JSON and Python entities while decoding
Please refer to the following conversion table, which is used by the json.load()
and json.loads()
method for the translations in decoding.
JSON | Python |
---|---|
object | dict |
array | list |
string | str |
number (int) | int |
number (real) | float |
true | True |
false | False |
null | None |
Python JSON conversion table
Now, let’s see the example. For this example, I am reading the “developer.json” file present on my hard drive. This file contains the following JSON data.
{
"name": "jane doe",
"salary": 9000,
"skills": [
"Raspberry pi",
"Machine Learning",
"Web Development"
],
"email": "JaneDoe@pynative.com",
"projects": [
"Python Data Mining",
"Python Data Science"
]
}
Developer JSON file
Example
Output:
Started Reading JSON file
Converting JSON encoded data into Python dictionary
Decoded JSON Data From File
name : jane doe
salary : 9000
skills : ['Raspberry pi', 'Machine Learning', 'Web Development']
email : JaneDoe@pynative.com
projects : ['Python Data Mining', 'Python Data Science']
Done reading json file
Access JSON data directly using key name
Use the following code If you want to access the JSON key directly instead of iterating the entire JSON from a file
Output:
Started Reading JSON file
Converting JSON encoded data into Python dictionary
Decoding JSON Data From File
Printing JSON values using key
jane doe
9000
['Raspberry pi', 'Machine Learning', 'Web Development']
JaneDoe@pynative.com
Done reading json file
You can read the JSON data from text, json, or a binary file using the same way mentioned above.
json.loads()
to convert JSON string to a dictionary
Sometimes we receive JSON response in string format. So to use it in our application, we need to convert JSON string into a Python dictionary. Using the json.loads()
method, we can deserialize native String, byte, or bytearray instance containing a JSON document to a Python dictionary. We can refer to the conversion table mentioned at the start of an article.
Output:
Started converting JSON string document to Python dictionary
Printing key and value
jane doe
9000
['Raspberry pi', 'Machine Learning', 'Web Development']
JaneDoe@pynative.com
['Python Data Mining', 'Python Data Science']
Done converting JSON string document to a dictionary
Parse and Retrieve nested JSON array key-values
Let’s assume that you’ve got a JSON response that looks like this:
developerInfo = """{
"id": 23,
"name": "jane doe",
"salary": 9000,
"email": "JaneDoe@pynative.com",
"experience": {"python":5, "data Science":2},
"projectinfo": [{"id":100, "name":"Data Mining"}]
}
"""
For example, You want to retrieve the project name from the developer info JSON array to get to know on which project he/she is working. Let’s see now how to read nested JSON array key-values.
In this example, we are using a developer info JSON array, which has project info and experience as nested JSON data.
Output:
Started reading nested JSON array
Project name: Data Mining
Experience: 5
Done reading nested JSON Array
Load JSON into an OrderedDict
OrderedDict can be used as an input to JSON. I mean, when you dump JSON into a file or string, we can pass OrderedDict to it.
But, when we want to maintain order, we load JSON data back to an OrderedDict so we can keep the order of the keys in the file.
As we already discussed in the article, a **object_pairs_hook**
parameter of a json.load()
method is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs.
Let’ see the example now.
Output:
Ordering keys
Type: <class 'collections.OrderedDict'>
OrderedDict([('John', 1), ('Emma', 2), ('Ault', 3), ('Brian', 4)])
How to use parse_float
and parse_int
kwarg of json.load()
As I already told parse_float
and parse_int
, both are optional parameters but, if specified, will be called with the string of every JSON float and integer to be decoded. By default, this is equivalent to float(num_str)
and int(num_str)
.
Suppose the JSON document contains many float values, and you want to round all float values to two decimal-point. In this case, we need to define a custom function that performs whatever rounding you desire. We can pass such a function to parse_float
kwarg.
Also, if you wanted to perform any operation on integer values, we could write a custom function and pass it to parse_int
kwarg. For example, you received leave days in the JSON document, and you want to calculate the salary to deduct.
Example
Output:
Load float and int values from JSON and manipulate it
Started Reading JSON file
Salary: 9250.542
<class 'float'>
Salary to deduct: 3
Done reading a JSON file
Implement a custom JSON decoder using json.load()
The built-in json module of Python can only handle Python primitives types that have a direct JSON equivalent (e.g., dictionary, lists, strings, numbers, None, etc.).
When you execute a json.load
or json.loads()
method, it returns a Python dictionary. If you want to convert JSON into a custom Python object then we can write a custom JSON decoder and pass it to the json.loads()
method so we can get a custom Class object instead of a dictionary.
Let’s see how to use the JSON decoder in the load method. In this example, we will see how to use **object_hook**
parameter of a load method.
Output:
After Converting JSON into Movie Object
Interstellar 2014 7000000
Also read:
- Check if a key exists in JSON and Iterate the JSON array
- Python Parse multiple JSON objects from file
So What Do You Think?
I want to hear from you. What do you think of this article? Or maybe I missed one of the uses of json.load()
and json.loads()
. Either way, let me know by leaving a comment below.
Also, try to solve the Python JSON Exercise to have a better understanding of Working with JSON Data in Python.