jsonify/pretty-print JSON for Bottle jsonify/pretty-print JSON for Bottle json json

jsonify/pretty-print JSON for Bottle


Thanks @Felk comment:set resopnse.content_typeto application/json.

def result():    response.content_type='application/json'    return data

or

def result():    return '<pre>{}</pre>'.format(json.dumps(data,             indent=4, default=json_util.default))

both will work for you.


I created the bottle-json-pretty plugin to extend the existing JSON dump done by Bottle.

I like being able to use the dictionary returned by my Bottle JSON/API functions in other template/view functions that return an actual page. Calling json.dumps or making a wrapper broke this since they would return the dumped str instead of a dict.

Example using bottle-json-pretty:

from bottle import Bottlefrom bottle_json_pretty import JSONPrettyPluginapp = Bottle(autojson=False)app.install(JSONPrettyPlugin(indent=2, pretty_production=True))@app.get('/')def bottle_api_test():    return {        'status': 'ok',        'code': 200,        'messages': [],        'result': {            'test': {                'working': True            }        }    }# You can now have pretty formatted JSON# and still use the dict in a template/view function# @app.get('/page')# @view('index')# def bottle_index():#     return bottle_api_test()app.run()