How to return data in JSON format using FastAPI?
The wrong approach If you serialise the object before returning it, using json.dumps() (as shown in your example), for instance: import json @app.get(‘/user’) async def get_user(): return json.dumps(some_dict, indent=4, default=str) the JSON object that is returned will end up being serialised twice, as FastAPI will automatically serialise the return value behind the scenes. Hence, the … Read more