If you scroll down a bit you can see this code from g.d.d.c return SQL table as JSON in python:
qry = "Select Id, Name, Artist, Album From MP3s Order By Name, Artist"
# Assumes conn is a database connection.
cursor = conn.cursor()
cursor.execute(qry)
rows = [x for x in cursor]
cols = [x[0] for x in cursor.description]
songs = []
for row in rows:
song = {}
for prop, val in zip(cols, row):
song[prop] = val
songs.append(song)
# Create a string representation of your array of songs.
songsJSON = json.dumps(songs)
I just want to keep the order of my columns.
For example when I print(cols)
I get this:
['id', 'Color', 'YCoord', 'Width', 'Height'] # right order
But the columns are saved in a wrong order:
[{"Color": "#FF99FF","Width"=345, "id"=43, "YCoord"=5784 "Height"=-546}...] # wrong order
The more columns I add, the more random it gets.