I have a fast api app with sqlite, I am trying to get an output as json which is valid. One of the columns in sqlite database is a list stored in Text column and another column has json data in Text column.
code sample below
database = Database("sqlite:///db/database.sqlite")
app = FastAPI()
@app.get("/flow_json")
async def get_data(select: str='*'):
query = query_formatter(table='api_flow_json',select=select)
logger.info(query)
results = await database.fetch_all(query=query)
print(results)
# this result is a list of tuples which i can confirm output stated below
return results
List of tuples printed
[('182', 'ABC', 'response_name', '[["ABC","DEF","GHI"]]', 'GHI', '{"metadata":{"contentId":"ABC"}}', '2', 'false', '39', '72', 'true')]
sqlite db row example below
"id","customer_name","response_name","entities","abstract","json_col","revision","disabled","customer_id","id2","auth"
182,"ABC","response_name","[[""ABC"",""DEF"",""GHI""]]","GHI","{""metadata"":{""contentId"":""ABC""}}",2,false,39,72,true
result using http call
[{"id":"182","customer_name":"ABC","response_name":"response_name","entities":"[[\"ABC\",\"DEF\",\"GHI\"]]","abstract":"GHI","json_col":"{\"metadata\":{\"contentId\":\"ABC\"}}","revision":"2","disabled":"false","customer_id":"39","id2":"72","auth":"true"}]
expected result
[{"id":"182","customer_name":"ABC","response_name":"response_name","entities":[["ABC","DEF","GHI"]],"abstract":"GHI","json_col":{metadata:{contentId:ABC}},"revision":"2","disabled":"false","customer_id":"39","id2":"72","auth":"true"}]
What did I try:
- transforming list to be more json friendly after I get the list of tuples
- tried the json1 extension for sqlite but doesn't work.
- I know that this will involve a formatting after response from database but can't figure out the formatting to return to client.