I have this table in postgres
CREATE TABLE target (
a json
)
Which I would like to insert data to from psycopg2 using
import psycopg2
import psycopg2.extras as extras
# a is list of list of dicts
a = [[{'a':1,'b':2}, {'d':3,'e':2}],
[{'a':3,'b':1}, {'e':2,'f':6}]]
# convert a to list of tuples containing extras.Json objects
a = [(extras.Json(x),) for x in a]
# insert a to the database
query = ('WITH ins (a) AS '
'(VALUES %s) '
'INSERT INTO target (a) '
'SELECT ins.a '
'FROM ins;')
cursor = conn.cursor()
extras.execute_values(cursor, query, a)
However I get the error: column "a" is of type json but expression is of type text
This example is simplified to make the question clearer (which is why the example is using the WITH ins statement as I need to join a table to it. I would have thought that this should work but for some reason it doesn't. My suspicion is the problem is that I load it in to ins first and maybe it loads it as a text instead of json there by default for some reason.
How do I solve this?