How do I return dict data directly instead of using json methods

Since I’m an api project, I don’t want to import json functions every time

from sanic import Sanic, json, Request

app = Sanic(__name__)


@app.post("/user")
async def create_user(request: Request):
    person = {"name": "bob", "age": 12}
    # return json(person)
    return person

from sanic import Sanic, Request, response.text

app = Sanic(__name__)


@app.post("/user")
async def create_user(request: Request):
    person = {"name": "bob", "age": 12}
    return response.text(str(person))

you do actually have to return a response object.

Documentation for this can be found here: Response | Sanic Framework

I should also like to point out that you can use a serializer from Sanic Extensions.

FWIW, this is actually a performance thing. By being explicit with a response object and not just returning anything, Sanic can skip some steps and just proceed to sending a response thus making the whole interaction faster.

Explicit is better than implicit.