Member-only story
This post is focused on different ways to interact with JSON with Python using json:
https://docs.python.org/3/library/json.html.
We’ll look at their limitations, run time errors, and etc., with sample code. We start from a basic type Dictionary and eventually discussed a solution for working with complex Python objects.
Dictionary
Python and the json module is working extremely well with dictionaries. The following is for serializing and deserializing a Python dictionary:
Code:
import jsonstudent = {
"first_name": "Jake",
"last_name": "Doyle"
}
json_data = json.dumps(student, indent=2)
print(json_data)
print(json.loads(json_data))
Output:
{
"first_name": "Jake",
"last_name": "Doyle"
}
{'first_name': 'Jake', 'last_name': 'Doyle'}
Object
However, if we create an object, e.g. Student (below), it doesn’t work out of the box.
Code:
import jsonclass Student(object):
def __init__(self, first_name: str, last_name: str):
self.first_name = first_name
self.last_name = last_name
student = Student(first_name="Jake", last_name="Doyle")
json_data = json.dumps(student)
Output:
TypeError: Object of type Student is not JSON serializable
But interestingly, there is the __dict__ on any python object, which is a dictionary used to store an object’s (writable) attributes. We can use that for working with JSON, and that works well.
Code:
import jsonclass Student(object):
def __init__(self, first_name: str, last_name: str):
self.first_name = first_name
self.last_name = last_name
student = Student(first_name="Jake", last_name="Doyle")
json_data = json.dumps(student.__dict__)
print(json_data)
print(Student(**json.loads(json_data)))
Output:
{"first_name": "Jake", "last_name": "Doyle"}
<__main__.Student object at 0x105ca7278>
The double asterisks **
in the Student(**json.load(json_data)
line may look confusing. But all it does is expanding the dictionary. In this case, it is equivalent to: