thedarklord47 thedarklord47 - 1 year ago 73
Python Question

return a list as JSON from web2py

Is there any way to return a list as a JSON list from web2py?

when I hit my route ending in

, web2py converts dictionaries into valid JSON. If the return value is a list however, it behaves unexpectedly

return ["test", "test"]




return [dict(test="test"), dict(test="test")]

breaks it completely (chrome complains with ERR_INVALID_CHUNKED_ENCODING). The expect behaviour is obviously the following valid JSON strings respectively:

["test", "test"]


[{"test":"test"}, {"test":"test"}]

Answer Source

registering a json service with the @service.json decorator and calling it with `app/controller/call/json/method' gave me the behaviour I expected:

def f():
    return ["test1", "test2"]

def call():
    return service()

The response body is

["test1", "test2"]

with content-type set to json

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download