Skip to content
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

Closed
guotie opened this issue May 17, 2012 · 15 comments
Closed

Can jsonify return array? #510

guotie opened this issue May 17, 2012 · 15 comments

Comments

@guotie
Copy link

guotie commented May 17, 2012

as the title says, can i use jsonify as this:

jsonify( [ { 'a': 1, 'b': 2 }, { 'c': 3, 'd': 4 } ] )

@davidshen84
Copy link

according to this article, flask will not allow you to return arrays directly. But I think flask should allow the user to control this.

@jfinkels
Copy link
Contributor

Instead, do this in Python

jsonify(items=[dict(a=1, b=2), dict(c=3, d=4)])

to get a JSON object which looks like this:

{"items": [{"a": 1, "b": 2}, {"c": 3, "d": 4}]}

@guotie
Copy link
Author

guotie commented Jun 3, 2012

yes, it can be done in python, but i think flask's jsonify should do it.

@guotie guotie closed this as completed Jun 3, 2012
@dhandeo
Copy link

dhandeo commented Nov 5, 2012

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 ?

@sigmavirus24
Copy link

First, everyone finding this issue should read the docs.

Second, if you insist upon returning arrays, jsonify won't do that. There is no "work around" to make jsonify return an array. What you will need to do is:

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))

@mizhi
Copy link

mizhi commented Nov 5, 2012

Are you trying to return the array to a browser or sending it as a parameter in a request?

@bjourne
Copy link

bjourne commented Mar 7, 2013

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.

@untitaker
Copy link
Contributor

@bjourne Did you read the doc linked above?

@jaredly
Copy link

jaredly commented Mar 24, 2015

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 disable_pre_es5_security (or something better named)?

@merqurio
Copy link

+1 @jaredly , I need it too

@untitaker
Copy link
Contributor

It (the restriction) will be removed, see #1182

@methane methane mentioned this issue Mar 25, 2015
6 tasks
@jhance
Copy link

jhance commented May 31, 2015

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)

@ajbraus
Copy link

ajbraus commented Oct 12, 2019

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

@untitaker
Copy link
Contributor

@ajbraus watch it. Your code is broken, not Flask. You try to serialize a type List, not the built-in type list.

@ThiefMaster
Copy link
Member

I don't see how Flask can claim to be a web server framework if this code doesn't work:

No need to be passive aggressive. 🙊

Have a look at Marshmallow for serializing your models to something that can be jsonified.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests