Can jsonify return array? #510

Closed
guotie opened this Issue May 17, 2012 · 12 comments

Comments

guotie commented May 17, 2012

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

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

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

Contributor

jfinkels commented May 21, 2012

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 commented Jun 3, 2012

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

@guotie guotie closed this Jun 3, 2012

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 ?

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 commented Nov 5, 2012

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

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.

Owner

untitaker commented Mar 7, 2013

@bjourne Did you read the doc linked above?

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

+1 @jaredly , I need it too

Owner

untitaker commented Mar 25, 2015

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

@methane methane referenced this issue Mar 25, 2015

Closed

Deprecate and remove APIs #1182

6 of 6 tasks complete

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment