-2

I am using Flask on python 2.7 and I want to return a value as a json value, I'm trying to do it as this,

    user_id = 100
    return_id = {}
    return_id['user'] = user_id

    return return_id

However I'm getting the following error,

Here is the full traceback,

127.0.0.1 - - [03/Oct/2016 15:04:16] "POST /add_new_patient HTTP/1.1" 500 - 127.0.0.1 - - [03/Oct/2016 15:04:30] "OPTIONS /add_new_patient HTTP/1.1" 200 - [2016-10-03 15:04:30,199] ERROR in app: Exception on /add_new_patient [POST] Traceback (most recent call last): File "/mnt/663E60F33E60BE25/Users/acer/Documents/code/thalback/venv/local/lib/python2.7/site-packages/flask/app.py", line 1988, in wsgi_app response = self.full_dispatch_request() File "/mnt/663E60F33E60BE25/Users/acer/Documents/code/thalback/venv/local/lib/python2.7/site-packages/flask/app.py", line 1642, in full_dispatch_request response = self.make_response(rv) File "/mnt/663E60F33E60BE25/Users/acer/Documents/code/thalback/venv/local/lib/python2.7/site-packages/flask/app.py", line 1746, in make_response rv = self.response_class.force_type(rv, request.environ) File "/mnt/663E60F33E60BE25/Users/acer/Documents/code/thalback/venv/local/lib/python2.7/site-packages/werkzeug/wrappers.py", line 847, in force_type response = BaseResponse(*_run_wsgi_app(response, environ)) File "/mnt/663E60F33E60BE25/Users/acer/Documents/code/thalback/venv/local/lib/python2.7/site-packages/werkzeug/test.py", line 871, in run_wsgi_app app_rv = app(environ, start_response) TypeError: 'dict' object is not callable

rksh
  • 3,920
  • 10
  • 49
  • 68
  • There's nothing wrong with that code. Can you add some more details to your question please? – Farhan.K Oct 03 '16 at 09:25
  • I tried your code, it works perfectly. Is there any other place in your code where you call the dictionary ? – MMF Oct 03 '16 at 09:26
  • do json.dumps() if it's an ajax call. – Amitkumar Karnik Oct 03 '16 at 09:27
  • 2
    You are attempting to call a dictionary, eg `some_dict()`, but it's impossible to be more specific without seeing the code that's causing this error, preferably a [mcve]. And you should also post the full error message, starting with the `traceback` line. – PM 2Ring Oct 03 '16 at 09:28
  • @PM2Ring added the full error – rksh Oct 03 '16 at 09:35

2 Answers2

6

While it's ok to return a string from a Flask handler with json.dumps, the preferred way is to use jsonify, which adds the correct Content-Type and accepts a variety of arguments, e.g. you can just call:

return jsonify(user=100)
bereal
  • 32,519
  • 6
  • 58
  • 104
  • Hi when I try to run the correct code it's not returning any output, ```return jsonify(user=user_id)1``` it doesn't return anything. However ```return jsonify(user='text')``` returns the value – rksh Oct 03 '16 at 10:01
  • @rksh I presume that the trailing `1` in your commend is a typo? – bereal Oct 03 '16 at 10:04
  • sorry yes typo, fixed and working thanks :) – rksh Oct 03 '16 at 10:07
2

I am using Flask on python 2.7 and I want to return a value as a json value

You are not returning a JSON string!

You are returning a python object. When Flask sees a python object, it tries to call it. When you return a dictionary, it assumes it is a callable and tries to call it.

What you need to do is:

import json

def your_function(..):
    ...
    return json.dumps(return_id)

PS: As correctly pointed out by @bereal in his answer, the above approach wont add Content-Type: application/json header in the response. You'd want to use jsonify(..) as shown in his answer.

UltraInstinct
  • 43,308
  • 12
  • 81
  • 104