I have a dictionary built from an SQL query ordered result, just before I jsonify() this result for output it's showing the following when I print to console (in correct order as I want it, sorted by the num key):
{'tradelist': [{'num': 0, 'trade': 1, 'type': 'entry long', 'signal': 'Long', 'date': '2017-01-31', 'price': 928.5, 'contracts': 109.1395}, {'num': 1, 'trade': 1, 'type': 'exit long', 'signal': 'Short', 'date': '2017-03-10', 'price': 1005.0, 'contracts': 109.1395}, {'num': 2, 'trade': 2, 'type': 'entry short', 'signal': 'Short', 'date': '2017-03-10', 'price': 1005.0, 'contracts': 84.0427}, {'num': 3, 'trade': 2, 'type': 'exit short', 'signal': 'Close entry(s) order Short', 'date': '2017-03-19', 'price': 970.0, 'contracts': 84.0427}]}
after that I do nothing else except a return jsonify(result) but when I have the query run over REST API, the output shows as follows:
{"tradelist":[{"contracts":109.1395,"date":"2017-01-31","num":0,"price":928.5,"signal":"Long","trade":1,"type":"entry long"},{"contracts":109.1395,"date":"2017-03-10","num":1,"price":1005.0,"signal":"Short","trade":1,"type":"exit long"},{"contracts":84.0427,"date":"2017-03-10","num":2,"price":1005.0,"signal":"Short","trade":2,"type":"entry short"},{"contracts":84.0427,"date":"2017-03-19","num":3,"price":970.0,"signal":"Close entry(s) order Short","trade":2,"type":"exit short"}]}
so it seems Jsonify is for some reason messing with the order and doing an alphabetical sort on the keys, any advise on how I can prevent this from happening
sql = "SELECT num, trade, type, signal, date, price, contracts from tradelist ORDER BY num"
query = conn.execute(sql)
result = {'tradelist': [dict(zip(tuple (query.keys()), row)) for row in query.cursor]}
print(result)
return jsonify(result)