Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Deserializing JSON Data

Give Feedback

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.

Tonya Sims on June 29, 2019

Great tutorial on JSON! Can you tell me which code editor you are using?

Matt L on July 29, 2019

Tonya - Austin seems to be using Visual Studio Code by Microsoft.

Andrew E on Dec. 11, 2019

Really enjoying this tutorial, thanks Austin :)

sroux53 on May 13, 2020

Excellent!

Become a Member to join the conversation.