I have a RESTful API, which returns all elements of a database table in a JSON format. I'd need to display the individual links/ids in the collection+json.
@app.route('/table/showall/<table>', methods = ['GET'])
def api_showAll(table) -> str:
if request.method == 'GET':
with DBcm.UseDatabase(DBconfig) as cursor:
_SQLlist = "SELECT * FROM %s;" % table
cursor.execute(_SQLlist)
data = cursor.fetchall()
Links = { "collection" :
{
"hrefs" :
[
{ "href": "127.0.0.1:5000/table/showone/tableNam/id"},
{ "href": "127.0.0.1:5000/table/showone/tableNam/id"},
{ "href": "127.0.0.1:5000/table/showone/tableName/id"}
]
}
}
return json.dumps(str(Links))
Since the data in the database will grow over time due to inserts, how would I dynamically add individual items into a JSON to show on the screen? Above you can see what I've tried so far. It shows 3 values hard-coded but I'd need to do it dynamically.
Any help appreciated