0

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})
5
  • Okay, so what is your problem/question? Commented Apr 9, 2018 at 17:34
  • Sorry @AlanHoover for not being clear I have updated my questions with a specific request. Commented Apr 9, 2018 at 17:38
  • what happens if you try to run what you posted? Commented Apr 9, 2018 at 17:40
  • I am getting an ` invalid syntax ` near my closing "]" Commented Apr 9, 2018 at 17:55
  • double that all of your opening and closing [],{}, and () match up up to that point Commented Apr 9, 2018 at 19:48

1 Answer 1

1

The posted code has some syntax errors, addressed below. However, to answer your question, yes. You want the DictCursor:

cur = conn.cursor( MySQLdb.cursors.DictCursor )
cur.execute('SELECT id, name FROM Players')
for row in cur.fetchall():
    print("{id}: {name}".format( row ))

Meanwhile, the posted code would work just fine, provided the syntax errors are corrected: the code is missing both a closing brace (}) and a closing bracket (]). After that, you'll like encounter a missing key problem with the dict().

things.append(dict([...

A dictionary is defined by key-value pairs. The code supplies an apparent value, but no key. Consider:

things.append(dict(mydata=[...

Or, eschew the extra lookup and function call by utilizing the language intrinsic:

things.append( {'mydata': [...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @hunteke for the recommendation. I removed the dict and just assigned the values from row into my append. The order of my data is a little wonky but that is a different story.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.