Hi Wendy - I got it!
It seems to be working perfectly.
I hope this example code can help save some users time. Please let me know if you have any suggestions for improvement.
=======================================================
import json
import mysql.connector
import requests
# Test Acct
API_KEY = “#############################################”
mydb = mysql.connector.connect(
host=“hostname”,
user=“username”,
passwd=“password”,
database=“database”
)
cursor = mydb.cursor()
# must be set to midnight UTC for the date you want
# had to convert the date to unix_timestamp, convert to midnight and then back to unix_timestamp *1000 for milliseconds
# Email must be the first column in select
select_stmt = “SELECT core_users.email, (unix_timestamp(DATE_FORMAT(unix_timestamp(core_users.createDate), ‘%Y-%m-%d 00:00:00’))*1000) AS mpc_create_date_2 FROM core_users WHERE (core_users.id in (117405,117406))”
cursor.execute(select_stmt)
# convert rows and columns to JSON
rows = cursor.fetchall()
json_email = {}
json_complete = []
# number of columns in query set
x = len(rows[0])
# loop through rows
for result in rows:
json_data = []
json_email1 = {}
json_email1[cursor.column_names[0]] = result[0]
# loop through query columns
for i in range(1, x):
json_data1 = {}
json_data1[‘property’] = cursor.column_names[i]
json_data1[‘value’] = result[i]
json_data.append(json_data1)
json_email1[‘properties’] = json_data
json_complete.append(json_email1)
json_email1 = {}
# output
print(json.dumps(json_complete))
url = “https://api.hubapi.com/contacts/v1/contact/batch”
querystring = {“hapikey”: “#############################################”}
payload = json.dumps(json_complete)
headers = {
‘Content-Type’: “application/json”,
}
response = requests.request(
“POST”, url, data=payload, headers=headers, params=querystring)
print(response.status_code)