I have a psql query that returns a json object. I fetch this query with results.fetchall()
and I get the json properly as a dict.
however, as I'm in python 3.4, not yet in 3.6, the objects' order is not preserved in the dict. I saw there's a way to use OrderedDict
to keep the order of the json but I'm not sure how to tell sqlalchemy/psycopg2 to use it.
can anybody help please?
As indicated in the documentation, you must provide a custom deserializer when creating your engine:
from functools import partial
import json, collections
engine = create_engine(
...,
json_deserializer=partial(
json.loads,
object_pairs_hook=collections.OrderedDict),
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With