JSON to Data Object in Python

Friday, February 14, 2020

I recently learned about Class methods. Here is a quick Class method to parse a JSON response into a Data object in Python.

@classmethod
def from_dict(cls, dict):
  obj = cls()
  obj.__dict__.update(dict)
  return obj

I’ve been using this in an application I wrote where I needed to parse SQS messages.

body_object = json.loads(message['Body'], object_hook=from_dict)
message_object = json.loads(body_object.Message, object_hook=from_dict)

I find it much easier to work with objects than dicts, but could just be me.

pythonclassdictionariesdata_objects

Quick API Auth for Flask

Quickly deploying Python code to Lambda