-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can jsonify return array? #510
Comments
according to this article, flask will not allow you to return arrays directly. But I think flask should allow the user to control this. |
Instead, do this in Python
to get a JSON object which looks like this:
|
yes, it can be done in python, but i think flask's jsonify should do it. |
I wanted to use https://github.com/hellais/jQuery-File-Upload which expects a json array. I would have liked to return it directly from jsonify. Can someone please let me know what is the work around ? |
First, everyone finding this issue should read the docs. Second, if you insist upon returning arrays, from json import dumps
from flask import Flask, make_response
app = Flask(__name__)
@app.route('/i_dont_listen_to_security_warnings')
def return_a_json_array_no_matter_what():
# craft your array
an_array_because_i_want_to = ['array', 'of', 'strings']
return make_response(dumps(an_array_because_i_want_to)) |
Are you trying to return the array to a browser or sending it as a parameter in a request? |
There are quite a few javascript libraries that expect server responses to be in the form of json arrays. That jsonify() doesn't let you return arrays makes it harder to integrate with them. |
@bjourne Did you read the doc linked above? |
This is fixed in ES5, and lots of sites no longer support browsers that are older than that. Can we have a flask option like |
+1 @jaredly , I need it too |
It (the restriction) will be removed, see #1182 |
This is a security flaw in javascript/browsers... If the API isn't being used from javascript, and json is just being used as a means of data transfer (eg. to some python client or something), then there isn't any point in users caring about the security issue. In addition its much easier to stream the data more efficiently if an array is returned (eg. in Haskell w/ pipes) |
How insane is this? jsonify does not serlialize a list into JSON...? that's insane I don't see how Flask can claim to be a web server framework if this code doesn't work: lists = List.query.all()
return jsonify(lists) or at the very least allow me to wrap the list in a dictionary and return it. lists = List.query.all()
return jsonify({"lists": lists}) But either of these returns TypeError: Object of type List is not JSON serializable |
@ajbraus watch it. Your code is broken, not Flask. You try to serialize a type List, not the built-in type list. |
No need to be passive aggressive. 🙊 Have a look at Marshmallow for serializing your models to something that can be jsonified. |
as the title says, can i use jsonify as this:
jsonify( [ { 'a': 1, 'b': 2 }, { 'c': 3, 'd': 4 } ] )
The text was updated successfully, but these errors were encountered: