How to "jsonify" a list in Flask?

Flask is currently causing an error while jsonizing a list.

I know there may be security reasons https://github.com/mitsuhiko/flask/issues/170 , but I still would like to have a way to return a JSON list as follows:

 [ {'a': 1, 'b': 2}, {'a': 5, 'b': 10} ] 

instead

 { 'results': [ {'a': 1, 'b': 2}, {'a': 5, 'b': 10} ]} 

when responding to application/json request. How can I return a JSON list in Flask using Jsonify?

+64
json flask
Sep 15
source share
8 answers

jsonify prevents this from being done for security reasons .

To do this, simply use json.dumps in the Python standard library.

http://docs.python.org/library/json.html#json.dumps

+45
Sep 16 '12 at 1:15
source share

You cannot, but you can do it anyway. I need this for jQuery-File-Upload

 import json # get this object from flask import Response #example data: js = [ { "name" : filename, "size" : st.st_size , "url" : url_for('show', filename=filename)} ] #then do this return Response(json.dumps(js), mimetype='application/json') 
+65
Oct 06
source share

This works for me. What version of Flask are you using?

 from flask jsonify ... @app.route('/test/json') def test_json(): list = [ {'a': 1, 'b': 2}, {'a': 5, 'b': 10} ] return jsonify(results = list) 
+8
Sep 15 '12 at 8:29
source share

Now the Flask jsonify() method serializes top-level arrays with this commit , available in Flask 0.11 and beyond.

For convenience, you can either go to the Python list: jsonify([1,2,3]) Or go to the args : jsonify(1,2,3) series

Both will be serialized into a top-level JSON array: [1,2,3]

Details here: http://flask.pocoo.org/docs/dev/api/#flask.json.jsonify

+7
Jan 25 '16 at 19:09
source share

Resolved, no fuss. You can be lazy and use jsonify, all you have to do is pass the elements = [your list].

Take a look here for a solution

https://github.com/mitsuhiko/flask/issues/510

+6
Oct 31 '14 at 17:17
source share

The list in the bulb can be easily jsonify using jsonify , for example:

 from flask import Flask,jsonify app = Flask(__name__) tasks = [ { 'id':1, 'task':'this is first task' }, { 'id':2, 'task':'this is another task' } ] @app.route('/app-name/api/v0.1/tasks',methods=['GET']) def get_tasks(): return jsonify({'tasks':tasks}) #will return the json if(__name__ == '__main__'): app.run(debug = True) 
+3
Mar 12 '16 at 14:15
source share

josonify works..but, if you are just going to pass an array without the "results" key, you can use the json library from python. The following conversion works for me.

  import json @app.route('/test/json') def test_json(): list = [ {'a': 1, 'b': 2}, {'a': 5, 'b': 10} ] return json.dumps(list)) 
0
Jan 07 '14 at
source share

If you are literally trying to return the JSON list in the bulb, and you are pretty sure that your variable is a list, then a simple way (where bin is a list of 1 and 0):

  return jsonify({'ans':bin}), 201 

Finally, in your client you will get something like

{"ans": [0.0, 0.0, 1.0, 1.0, 0.0]}

0
Jul 06 '17 at 6:04
source share



All Articles