How do I return a JSON array with Bottle? How do I return a JSON array with Bottle? python python

How do I return a JSON array with Bottle?


Bottle's JSON plugin expects only dicts to be returned - not arrays. There are vulnerabilities associated with returning JSON arrays - see for example this post about JSON hijacking.

If you really need to do this, it can be done, e.g.

@route('/array')def returnarray():    from bottle import response    from json import dumps    rv = [{ "id": 1, "name": "Test Item 1" }, { "id": 2, "name": "Test Item 2" }]    response.content_type = 'application/json'    return dumps(rv)


According to Bottle's 0.12 documentation:

As mentioned above, Python dictionaries (or subclasses thereof) are automatically transformed into JSON strings and returned to the browser with the Content-Type header set to application/json. This makes it easy to implement json-based APIs. Data formats other than json are supported too. See the tutorial-output-filter to learn more.

Which means you don't need to import json nor setting the content_type attribute of the response.

Thus, the code gets hugely reduced:

@route('/array')def returnarray():    rv = [{ "id": 1, "name": "Test Item 1" }, { "id": 2, "name": "Test Item 2" }]    return dict(data=rv)

And the JSON document returned by the Web server would look like:

{"data": [{"id": 1, "name": "Test Item 1"}, {"id": 2, "name": "Test Item 2"}]}