1

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.