I have a table in postgresql with a JSON type column. I am trying to add data to a table.
cursor.execute("""INSERT INTO my_table VALUES(%s);""",(json.dumps(myobject))
Worked like a charm. But now I need to really increase the bandwidth. Here is the code that does not work:
import StringIO,psycopg2,json buffer = StringIO.StringIO(json.dumps(myobject)) cursor.copy_from(buffer,'my_table') connection.commit()
json written to the buffer is not compatible with copy_from. For example, the characters "\" must be escaped, so "\ n" must be "\\ n".
How can I write a string to the buffer so copy_from puts the correct json in my table?
thanks
source share