How to convert SQLAlchemy orm object result to JSON format?
Currently I am using sqlalchemy reflection to reflect tables from the DB. Consider I have a User table an
Despite "doog adibies" answer has been accepted and I upvoted it since has been extremely helpful, there are a couple of notable issues in the algorithm:
found
")Father
object with a relationship to Son
with a configured backref
, you will generate an extra Father
node for each son in it, with the same data that the main Father
object already provides!)To fix these issues, I defined another set()
to track undesired back relationships and I moved the tracking of visited children later in the code. I also deliberately renamed variables in order to make more clear (of course IMO) what they represents and how the algorithm works and replaced the map()
with a cleaner dictionary comprehension.
The following is my actual working implementation, which has been tested against nested objects of 4 dimensions (User -> UserProject -> UserProjectEntity -> UserProjectEntityField):
def model_to_dict(obj, visited_children=None, back_relationships=None):
if visited_children is None:
visited_children = set()
if back_relationships is None:
back_relationships = set()
serialized_data = {c.key: getattr(obj, c.key) for c in obj.__table__.columns}
relationships = class_mapper(obj.__class__).relationships
visitable_relationships = [(name, rel) for name, rel in relationships.items() if name not in back_relationships]
for name, relation in visitable_relationships:
if relation.backref:
back_relationships.add(relation.backref)
relationship_children = getattr(obj, name)
if relationship_children is not None:
if relation.uselist:
children = []
for child in [c for c in relationship_children if c not in visited_children]:
visited_children.add(child)
children.append(model_to_dict(child, visited_children, back_relationships))
serialized_data[name] = children
else:
serialized_data[name] = model_to_dict(relationship_children, visited_children, back_relationships)
return serialized_data
You can use the relationships property of the mapper. The code choices depend on how you want to map your data and how your relationships look. If you have a lot of recursive relationships, you may want to use a max_depth counter. My example below uses a set of relationships to prevent a recursive loop. You could eliminate the recursion entirely if you only plan to go down one in depth, but you did say "and so on".
def object_to_dict(obj, found=None):
if found is None:
found = set()
mapper = class_mapper(obj.__class__)
columns = [column.key for column in mapper.columns]
get_key_value = lambda c: (c, getattr(obj, c).isoformat()) if isinstance(getattr(obj, c), datetime) else (c, getattr(obj, c))
out = dict(map(get_key_value, columns))
for name, relation in mapper.relationships.items():
if relation not in found:
found.add(relation)
related_obj = getattr(obj, name)
if related_obj is not None:
if relation.uselist:
out[name] = [object_to_dict(child, found) for child in related_obj]
else:
out[name] = object_to_dict(related_obj, found)
return out
Also, be aware that there are performance issues to consider. You may want to use options such as joinedload or subqueryload in order to prevent executing an excessive number of SQL queries.