Search code examples
pythonjsonflaskjsonify

How to return a json response in one line in flask?


I am coding a route in flask to send this response and I need to send this json response in a single solid line but when I send this json(because of length) send in multiple line

@app.route("/dashboard", methods= ['GET','POST'])
def index():
.
.
.
response = {"iDisplayStart":1,"iDisplayLength":10,
"iTotalRecords":2,"iTotalDisplayRecords":2,"data":data_ne}

return jsonify(results)

when I send this json response return below

 {
  "iDisplayStart": 1, 
  "iDisplayLength": 10, 
  "iTotalRecords": 2, 
  "iTotalDisplayRecords": 2, 
  "data": [
  {data_ne}]}

but I want to be in single line like below

  {"iDisplayStart":1,"iDisplayLength":10,"iTotalRecords":2,"iTotalDisplayRecords":2,"data":data_ne}

Is there any way to do this? Thanks for your consideration.


Solution

  • Are you running Flask in debug mode? Try setting JSONIFY_PRETTYPRINT_REGULAR environment variable to false. From the documentation:

    The default output omits indents and spaces after separators. In debug mode or if JSONIFY_PRETTYPRINT_REGULAR is True, the output will be formatted to be easier to read.