thedarklord47 thedarklord47 - 23 days ago 8
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

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

return ["test", "test"]


displays

testtest


and

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"]


and

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

Answer

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