0

I have a database from which I get user data and write it to a json file.

This is structure:

The problem is that I get the value right away. How to get a key, too, not like this

[
    54,
    "newuser",
    "newuser",
    "newuser@mail.ru",
    "2020-06-30",
    "qweqwe123",
    123123123,
    "description"
],

but just like this

{
    "id": "54",
    "firstname": "newuser",
    "lastname": "newuser",
    "email": "newuser@mail.ru",
    "born_date": "qweqwe123",
    "password": "34962",
    "phone_number": "123123123",
    "description": "description"
},

code:

@app.route('/register', methods=['GET', 'POST'])
def register():
    cur = mysql.connection.cursor()
    cur.execute("SELECT * FROM users.data")
    account = cur.fetchall()
    description = account
    content = {}
    content = description
    with open('data_json.json', 'w') as json_file:
        json.dump(content, json_file, indent=4)
    
    return render_template('profile.html')
abby
  • 1
  • 1
  • 4
  • 2
    Does this answer your question? [Python, MySQL and SELECT output to dictionary with column names for keys](https://stackoverflow.com/questions/7268178/python-mysql-and-select-output-to-dictionary-with-column-names-for-keys) – azro Jul 28 '20 at 17:12
  • Do `mysql.connection.cursor(dictionary=True)` – azro Jul 28 '20 at 17:13
  • @azro I got this error `TypeError: cursor() got an unexpected keyword argument 'dictionary'` – abby Jul 28 '20 at 17:19
  • @azro, could you please help me to solve this issue – abby Jul 29 '20 at 10:44
  • What is `mysql` in your code ? Is that directly the lib ? How do you connect ? What is the lib you use ? – azro Jul 29 '20 at 17:12

0 Answers0