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
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 580 Dec-18-2025, 01:54 AM
Last Post: DavidSah
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 914 Nov-17-2025, 11:47 AM
Last Post: Winfried
Question [openpyxl] Set the locking property of all cells BelleroDev 3 1,089 Nov-07-2025, 11:59 AM
Last Post: Pedroski55
  Replace values in Yaml file with value in dictionary PelleH 1 3,817 Feb-11-2025, 09:51 AM
Last Post: alexjordan
Question Using Lists as Dictionary Values bfallert 8 3,938 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  need to compare 2 values in a nested dictionary jss 2 2,974 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 3,856 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 7,585 Jan-24-2023, 08:22 AM
Last Post: perfringo
  problem adding two numpy arrays djf123 2 3,458 Aug-09-2022, 08:31 PM
Last Post: deanhystad
  Django: Adding Row Data To Existing Model Instance Question/Problem. Steven_Pinkerton 1 2,356 Aug-09-2022, 10:46 AM
Last Post: Addweb

Forum Jump:

User Panel Messages

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