Gebruik gewoon json.dumps
voor json-gegevens (serialiseert naar string) zoals vermeld in de docs
en laat psycopg2
doe al het werk en parameterbinding:
cattle_id = 'cattle_A'
step_count_dict = json.dumps({1: 22, 4: 12})
speed_dict = json.dumps({2: 24, 6: 98})
cur = con.cursor()
query = "INSERT INTO global_records(cattle_id, step_count, speed) VALUES (%s, %s, %s)"
cur.execute(query, (cattle_id, step_count_dict, speed_dict))
con.commit()
cur.execute('Select * from global_records')
print(cur.fetchall())
Uit:
[('cattle_A', {'1': 22, '4': 12}, {'2': 24, '6': 98})]