2

I have to pass a json object to a function but it's not working:

import psycopg2
cur.execute(" SELECT mrp_sp_insert_jobdef( %s )",(json(_jobdef),) )

TypeError: 'module' object is not callable

Alternatively:

cur.execute(" SELECT mrp_sp_insert_jobdef( % )",(json.dumps(_jobdef),) )

ValueError: unsupported format character ' ' (0x20) at index 31

1 Answer 1

2

You can use the wrapper psycopg2.extras.Json(adapted, dumps=None), example:

import psycopg2
from psycopg2.extras import Json

conn = psycopg2.connect("dbname=test user=postgres password=password")
cur = conn.cursor()

_jobdef = {'id': 1, 'name': 'product', 'amount': 230}
cur.execute("SELECT mrp_sp_insert_jobdef( %s )", (Json(_jobdef),) )
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.