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)
Formatting MySQL results in the correct property/value JSON format for Hubpot
SOLVE
I am now much closer. The problem was that I needed to move this inside the column loop: json_data1 = {}.
This works perfectly for a single record, but now the rows are overwriting each other because the key name "email" looks like a duplicate, so instead of adding a new dictionary it is updating the existing dictionary.
I think I've read every topic written the the web trying to figure this out...
I'm at a complete loss now....
# CODE WORKS PERFECTLY FOR A SINGLE ROW. FOR MULTIPLE ROWS IT OVERWRITES AND ONLY INCLUDES THE LAST RECORD VALUES.
import json import mysql.connector # from hubspot3.contacts import ContactsClient
# Test Acct API_KEY = "#############################################"
for result in rows: json_data = [] json_email1 = {}
json_email1[cursor.column_names[0]] = result[0]
for i in range(1, x): json_data1 = {} #<-- THIS FIXED THE FIRST PROBLEM json_data1['property'] = cursor.column_names[i] json_data1['value'] = result[i] json_data.append(json_data1)
json_email1['properties'] = json_data json_email.update(json_email1) #<--- THIS IS THE PROBLEM. OVERWRITES INSTEAD OF ADDING NEW DICT. json_email1 = {}
Digging deeper into this <here>, it looks to me that you're pulling only one data and that is user 117405? Could this be the reason why you're seeing overwrites?
If that's not it, I'm also curious to learn why did you place
json_email1[cursor.column_names[0]] = result[0]
above the for loop instead of looping through all the return response?
Formatting MySQL results in the correct property/value JSON format for Hubpot
SOLVE
I had that one user there for testing. It was overwriting because I wasn't putting it in a list and was trying to put it just in a dictionary which wouldn't accept another dict with the same key - but does in a list, and I realzied that the Hubspot requirement does have all the results in a list which I missed the first time I looked at it.
I put this here:
json_email1[cursor.column_names[0]] = result[0]
to pull out the emai which was the primary key before the rest of the data which is in a list.
Then loop through the rest of the results from [1] to [x].
Just have to make sure to put the email as the first column in the query.
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)