Sqlalchemy / psycopg2 desreializes my jsons as dicts but doesn't preserve order

I have a psql query that returns a json object. I am getting this request with results.fetchall()

and I understand json as a dict correctly. however, since I am in python 3.4, not yet 3.6, the order of the objects is not preserved in the dict. I saw there a way to use OrderedDict

to keep the json order, but I'm not sure how to tell sqlalchemy / psycopg2 to use it.

Can anyone please help?

+3


source to share


1 answer


As stated in the documentation , you must specify your own 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),
    )

      

+2


source







All Articles