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
.
Tonya Sims on June 29, 2019
Great tutorial on JSON! Can you tell me which code editor you are using?