Ask Your Question
1

How can psycopg2.extras.execute_values be used to input a JSON array into Postgres?

asked 2023-01-03 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-08-01 05:00:00 +0000

qstack gravatar image

To use execute_values to insert a JSON array into Postgres, you can follow these steps:

  1. Convert your JSON array into a list of tuples, where each tuple represents a row to be inserted into the table. For example:
import json
import psycopg2

json_data = '[{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]'
data = json.loads(json_data)
values = [(json.dumps(row),) for row in data]

In this example, we are loading a JSON array of objects into Python and converting each object into a JSON string before adding it to the list of tuples. Note that the json.dumps function is used to convert the object to a JSON string.

  1. Use execute_values to insert the data into the table. For example:
conn = psycopg2.connect(database="mydb", user="myuser", password="mypassword", host="localhost", port="5432")
cur = conn.cursor()

sql = "INSERT INTO mytable (myjsoncolumn) VALUES %s"
psycopg2.extras.execute_values(cur, sql, values)

conn.commit()
cur.close()
conn.close()

In this example, we are executing a SQL statement that inserts a list of JSON strings into a column named myjsoncolumn in a table named mytable. The %s placeholder is used to signal where the data should be inserted in the query. We pass the list of tuples containing the data to execute_values, along with the SQL query and the cursor object. Finally, we commit the transaction and close the cursor and connection objects.

Note that execute_values automatically handles batching and parameterization, which can greatly improve the performance of your insertions.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-01-03 11:00:00 +0000

Seen: 10 times

Last updated: Aug 01 '21