If I am only returning one row from my mysql query and I know the headers can I assign those values via dict in python? I do have some data that is nested. The data will eventually be returned as a JSON object.
Question: What is the correct syntax to assign those mysql values into my python array things and return that object as a json string?
For example my JSON would need to look something like you will notice the relative data in the query is nested for the school and address:
{
“data”:[
{
“player”:
{
“id”:xxx-xxx-xxx,
“name”: “name1”,
“school”:
{
“id”:xxxx,
“address”:
{
“id”:xxx,
“city”: “mycity”,
“state”: “OH”
},
“name”: “myschool”,
“mascot”: “wildcats”
},
“height”: 72,
“weight”: 180
},
]
}
And I want to be able to assign values similar to:
cur.execute("select pla.id, pla.name, sch.id, address.id, address.city, address.state, sch.name, sch.mascot, pla.height, pla.weight FROM Players pla LEFT JOIN SchoolData sch ON pla.School = sch.ID LEFT JOIN LocationData address ON sch.Location = address.ID where pla.ID = %d" %int(student_id))
things = []
for row in cur.fetchall():
things.append(dict([
{"data":[
{
"player":
{
"id":row[0],
"name": row[1],
"school":
{
"id":row[2],
"address":
{
"id":row[3],
"city": row[4],
"state": row[5]
},
"name": row[6],
"mascot": row[7]
},
"height": row[8],
"weight": row[9]
},
]
}
]))
return (jsonify({'players:' : things})
[],{}, and()match up up to that point