Deserializing JSON Data
In this video, you’ll learn how to deserialize JSON data into Python objects you can use in your program.
The json
module exposes two methods for deserializing JSON
load()
will load JSON data from a file-like object. We use this method when we’re reading in data from a file-like object.
loads()
will load JSON data from a string containing JSON-encoded data.
Unless your encoded data is something very simple, these methods will most likely return a Python dict
or list
containing your deserialized data.
This chart shows how JSON data is deserialized into Python objects
JSON | Python |
---|---|
object | dict |
array | list |
string | str |
number (int) | int |
number (real) | float |
true | True |
false | False |
null | None |
Serialization and Deserialization are not perfectly inverse operations! This means that deserialization may not return to you the exact object you serialized.
For example, a Python tuple
will be serialized as a JSON array
. When we deserialize the array
, we will get a Python list
containing the data in the tuple. If we want our original tuple
object back, we need to pass this list into the initializer for the tuple
.
00:00 Welcome back to our series on working with JSON data in Python. In this video, we’re going to learn how to deserialize JSON data back into native Python objects. Before we start coding, we have to learn how JSON is converted back into Python.
00:16
JSON objects become Python dictionaries, arrays become lists, and null data is represented as NoneType
in Python. Integers remain the same, but any other type of real number becomes a Python float
.
00:30 Now, you might think that this is just the inverse operation of converting Python objects to JSON, but unfortunately it’s not 100% perfect. Serialization and deserialization are not perfectly inverse operations.
00:46 This means that deserialization may not return to you the exact object you serialized. In addition, deserialization may not give us back all of the required metadata we need to reconstruct our original object.
01:00 We’ll see more of that later on. Our example of deserialization will return object data in the form of a Python list, which is not the actual object we desire.
01:11
Let’s take a look. I’m here in a Python shell and I want to demonstrate how serialization and deserialization are not perfectly inverse operations. I’m going to start by importing the json
module. There we go.
01:27
And now I’m going to create a new tuple called blackjack_hand
with the integer 8
and the string "Q"
. And now I want to encode this into a JSON string called encoded_hand
, so I’ll call the dumps()
method from the json
module and I’ll pass in our blackjack_hand
tuple. Now let’s decode this JSON string.
01:51
I’ll make a new variable called decoded_hand
and I’ll type json.loads()
, passing in encoded_hand
. The load()
and loads()
methods are used to deserialize JSON data from either a file-like object or a string, similar to the dump()
and dumps()
methods.
02:11
Let’s inspect our decoded elements type. I’ll write type(decoded_hand)
and we’ll see that the class is a list
. That’s because the deserializer returned the data for our tuple object, but it’s not actually the tuple object itself. I’ll type decoded_hand
and hit Enter, and there we can see the list. So now if I compare the original blackjack_hand
, which is a tuple
, to a new tuple object from this list, we’ll see that these objects have the same value.
02:46 This tells us that if we want to get our original object back, we’re going to have to pass the deserialized data into that object’s constructor, or initializer. In the next video, we’ll take a look at how we can process a large amount of JSON data to derive meaning from it.
Matt L on July 29, 2019
Andrew E on Dec. 11, 2019
Really enjoying this tutorial, thanks Austin :)
sroux53 on May 13, 2020
Excellent!
Aeliana on Aug. 7, 2023
I have a problem regarding requests. I don’t know how to resolve it. I am using visual studio code and I am a beginner. Please see below:
import json
import requests
response = requests.get("https://jsonplaceholder.typicode.com/todos/1")
todos = json.loads(response.text)
print(todos[:2])
import requests
ModuleNotFoundError: No module named 'requests'
Bartosz Zaczyński RP Team on Aug. 7, 2023
@Aeliana The requests
module doesn’t come with Python, which means that you need to install it using pip
into your project’s virtual environment or, if you don’t mind, into the global Python interpreter that shipped with your operating system:
$ python3 -m pip install requests
In Visual Studio Code, you can also open up the terminal window (Ctrl + `) and install the module there, same as above.
Once installed, you should be able to import requests
in your Python scripts.
Become a Member to join the conversation.
Tonya Sims on June 29, 2019
Great tutorial on JSON! Can you tell me which code editor you are using?