row_to_json data gets interpreted
Reported by Psycopg website | July 19th, 2013 @ 04:28 PM
Submitted by: eric casteleijn
Recent versions of Postgres allow returning json from queries. Psycop since version 2.5.0 decided to call json.loads on the query results for some mysterious reason. This is not psycops responsibiblity: if I query the database, and ask for json, I expect json back, not json interpreted into python structures. (One of the reasons to use this postgres features is to return json responses directly from the database to a webserver. Any advantages of doing that are lost if you need to call json.dumps again after psycop calls json.loads.)
Comments and changes to this ticket
Mmm, i think you are right, the choice of adapting automatically json is questionable.
Eric, while we decide how to fix the problem you can disable the automatic adaptation using:
import psycopg2.extensions as ext ext.string_types.pop(ext.JSON.values, None) ext.string_types.pop(ext.JSONARRAY.values, None)
It's enough to do it only once and is idempotent anyway. If you guard it with an
if hasattr(ext, 'JSON')it would work with any psycopg release.
I've written to the mailing list my ideas about the issue. Feel free to take part to the discussion.
The automatic cast of json was introduced in a major release (2.5): although the choice can be questioned (not that anybody did) it was done in a legitimate moment. Dropping automatic casting in 2.5.x would be a bad reaction; the right moment to think about a different behaviour is for 2.6.
You have plenty of options to work around the issue:
- use psycopg < 2.4.x, which is still being maintained
pip install "psycopg2<2.5"will give you the most up-to-date)
- cast your json as text in the query
- use the above snippet to unregister the json typecaster.
Sorry for the inconvenience.
- use psycopg < 2.4.x, which is still being maintained (
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Psycopg is the most used PostgreSQL adapter for the Python programming language. At the core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.