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…