Python Forum
Problem adding keys/values to dictionary where keynames = "property" and "value"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem adding keys/values to dictionary where keynames = "property" and "value"
#1
See my code below. After about 30 hours I am close, but cannot figure out how to add a new {key , value} for each column in the mysql result set.
I am looping through each column but as the values write they overwrite each time and I end up with the value of the last column for all the keys.

My goal is this result:
{"email": "[email protected]", "properties": [{"property": "firstname", "value": "Codey"},{"property": "lastname","value": "Huang"}]}

but we are getting this:
{"email": "[email protected]", "properties": [{"property": "lastname", "value": "Huang"}, {"property": "lastname", "value": "Huang"}]}

import json
import mysql.connector
# from hubspot3.contacts import ContactsClient

# Test Acct
API_KEY = "#############################################"

mydb = mysql.connector.connect(
host="hostname",
user="username",
passwd="password",
database="database"
)

cursor = mydb.cursor()
select_stmt = "SELECT core_users.email, core_users.firstname, core_users.lastname FROM core_users WHERE (core_users.id=%(user)s)"
cursor.execute(select_stmt, {'user': 117405})

rows = cursor.fetchall()
json_email = {}
json_data1 = {}
json_data = []

# number columns in query set
x = len(rows[0])

for result in rows:

json_email[cursor.column_names[0]] = result[0]

i = 1
while i < x:

json_data1['property'] = cursor.column_names[i]
json_data1['value'] = result[i]
i = i + 1
json_data.append(json_data1)

json_email['properties'] = json_data

print(json.dumps(json_email))

Thanks!
Jason
Reply
#2
I found the answer. The json_data1 = {} just needed to be moved inside the loop. Yay!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 284 Jun-15-2021, 03:48 PM
Last Post: snippsat
  Getting values from a dictionary brunolelli 5 762 Mar-31-2021, 11:57 PM
Last Post: snippsat
  Python dictionary with values as list to CSV Sritej26 4 624 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 930 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0 archanut 2 414 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 566 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Adding keys and values to a dictionary giladal 3 596 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  extracting second values elements only in every keys in an OrderedDict glennford49 4 618 Nov-12-2020, 08:22 AM
Last Post: glennford49
  Can property getters and setters have additional arguments? pjfarley3 2 634 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  In this dictionary all the values end up the same. How? Pedroski55 2 542 Oct-29-2020, 12:32 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020