0

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

hasumedic
  • 2,139
  • 12
  • 17
Kanyenke
  • 3
  • 4

1 Answers1

0

Make a class which replicates your model and make it json serializable and then you can json.dumps(object) , you can find a possible solution code here How to make a class JSON serializable

Community
  • 1
  • 1
Code2end
  • 116
  • 8