简体   繁体   中英

Psycopg2 invalid json when returning postgres query

I am using Django to access a stored function in my postgres DB. When I execute the function inside of Postgres, it returns doublequotes and valid json. However, when I call the function from Django (which uses psycopg2) the doublequotes are removed and single quotes replace them.

It seems psycopg2 is doing some type of conversion to lists / dictionary in the background. However, I need to keep the json. Any ideas how to resolve this?

You can override the functionality of psycopg2 auto converting the JSON object/array by registering a no-op function with register_default_json()

psycopg2.extras.register_default_json(loads=lambda x: x)

Quote from the Docs

Psycopg automatically converts PostgreSQL json data into Python objects. How can I receive strings instead? The easiest way to avoid JSON parsing is to register a no-op function with register_default_json():

psycopg2.extras.register_default_json(loads=lambda x: x) See JSON adaptation for further details.

Additional Reading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM