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)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)