Python Programming Tips

The best approach to serialise/deserialise between Python items and JSON — Attr and Cattr

Photo via JoshuaWoroniecki on Pixabay
pip set up attrs
pip set up cattrs
from attr import attrs, attrib
import cattr
Photo via Free-Photos on Pixabay
from typing import[1.01, True, False, "2", "I will be ignored"], Tuple[int, int, int, int])
Photo via PublicDomainPictures on Pixabay
magnificence Person:
identify = attrib()
age = attrib()
def tell_age(self):
print(f'My identify is {self.identify} and my age is {self.age}.')
from attr import asdictp1 = Person('Chris', 32)
p2 ={'identify': 'Chelsea', 'age': 1}, Person)
Photo via josephredfield on Pixabay
p_list_raw = [
{'identify': 'Alice', 'age': 20},
{'identify': 'Bob', 'age': 25},
{'identify': 'Chris', 'age': 32}
from typing import Listp_list =, List[Person])
Photo via SCY on Pixabay
import jsonjson_obj = json.dumps(cattr.unstructure(p_list))
p_list =, List[Person])
Photo via geralt on Pixabay


Please enter your comment!
Please enter your name here