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
  need to compare 2 values in a nested dictionary jss 2 797 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 1,317 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,513 Jan-24-2023, 08:22 AM
Last Post: perfringo
  problem adding two numpy arrays djf123 2 2,042 Aug-09-2022, 08:31 PM
Last Post: deanhystad
  Django: Adding Row Data To Existing Model Instance Question/Problem. Steven_Pinkerton 1 1,214 Aug-09-2022, 10:46 AM
Last Post: Addweb
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,680 Feb-07-2022, 07:29 PM
Last Post: saavedra29
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 1,695 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,495 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 3,255 Jun-15-2021, 03:48 PM
Last Post: snippsat
  Getting values from a dictionary brunolelli 5 3,518 Mar-31-2021, 11:57 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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