I am writing a a REST API in python3 using flask and I have struggled to find a way to return a dict as json while maintaining the order of the keys. I am using an OrderedDict because obviously the built in dictionary does not preserve order of elements.
I have a method that looks like this:
@app.route('/foobar', methods=['GET'])
def foobar()
data = [OrderedDict([("a",1),
("b",2),
("c",3)])]
return jsonify(data),200
And here is an example of the output I could get:
[{"b":2,"c":3,"a":1}]
This is what I want to get:
[{"a":1,"b":2,"c":3}]
How can I return JSON in the order that its defined in the ordered dict?
Edit: This is not a duplicate because the linked post is only for a dictionary I need this to work for a list of dictionaries