-1

I want to send this list as JSON in the Flask framework.

I fetched something from my DB and I have this here as my list:

[('GG',)]

How do I send it to JSON?

@app.route('/gt', methods=['GET'])
def gt():
    logged_in_email = session['auth']
    if not logged_in_email:
        return "Nothing"
    else:
        r = todo.retrieve_todo(logged_in_email)
        print(r)
        print(type(r))
        return r

This doesn't work, I tried json.loads but that expects a string and I have a list?

garoo
  • 163
  • 1
  • 2
  • 10

2 Answers2

1

Using jsonify you can return json result easily.

from flask import jsonify

@app.route('/gt', methods=['GET'])
def gt():
    logged_in_email = session['auth']
    if not logged_in_email:
        return "Nothing"
    else:
        r = todo.retrieve_todo(logged_in_email)
        return jsonify(r=r)

Or if you want to return str, use json.dumps. json.loads converts str to list or dict.

yslee
  • 236
  • 1
  • 12
1

I did it like this....

from flask import jsonify

@app.route('/', methods=['GET'])
def json_sample():
    return jsonify([1, 2, "test"])
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Alex028502
  • 3,486
  • 2
  • 23
  • 50