I am using the python library python-twitter to access the twitter api.
I am doing a search query and want that response to go my flask route.
I get this erro
TypeError: <twitter.status.Status object at 0x7f16dc089110> is not JSON serializable
Here is what I have
@app.route('/t/search/tweets', methods=['GET'])
def twit():
results = twitter_api.GetSearch(count=count, term='hello world')
return Response(json.dumps(results), mimetype='application/json')
Here is the full trackback
Traceback (most recent call last):
File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/john/Scripts/Python/in5/views.py", line 73, in twit
return Response(json.dumps(results), mimetype='application/json')
File "/usr/lib/python2.7/json/__init__.py", line 243, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <twitter.status.Status object at 0x7fc3d3384390> is not JSON serializable
And here is the 'print results'
[<twitter.status.Status object at 0x7ff1f3114f90>, <twitter.status.Status object at 0x7ff1f30c6050>, <twitter.status.Status object at 0x7ff1f30c6190>, <twitter.status.Status object at 0x7ff1f30c6310>, <twitter.status.Status object at 0x7ff1f30c6510>, <twitter.status.Status object at 0x7ff1f30c65d0>, <twitter.status.Status object at 0x7ff1f30c6690>, <twitter.status.Status object at 0x7ff1f30c67d0>, <twitter.status.Status object at 0x7ff1f30c69d0>, <twitter.status.Status object at 0x7ff1f30c6c10>, <twitter.status.Status object at 0x7ff1f30c6d90>, <twitter.status.Status object at 0x7ff1f30c6e50>, <twitter.status.Status object at 0x7ff1f30c90d0>, <twitter.status.Status object at 0x7ff1f30c91d0>, <twitter.status.Status object at 0x7ff1f30c9290>, <twitter.status.Status object at 0x7ff1f30c9410>, <twitter.status.Status object at 0x7ff1f30c95d0>, <twitter.status.Status object at 0x7ff1f30c9690>, <twitter.status.Status object at 0x7ff1f30c9750>, <twitter.status.Status object at 0x7ff1f30c9a10>, <twitter.status.Status object at 0x7ff1f30c9ad0>, <twitter.status.Status object at 0x7ff1f30c9c10>, <twitter.status.Status object at 0x7ff1f30c9dd0>, <twitter.status.Status object at 0x7ff1f30c9e90>, <twitter.status.Status object at 0x7ff1f30c9f90>, <twitter.status.Status object at 0x7ff1f30cf1d0>, <twitter.status.Status object at 0x7ff1f30cf290>, <twitter.status.Status object at 0x7ff1f30cf510>, <twitter.status.Status object at 0x7ff1f30cf5d0>, <twitter.status.Status object at 0x7ff1f30cf690>, <twitter.status.Status object at 0x7ff1f30cf810>, <twitter.status.Status object at 0x7ff1f30cf8d0>, <twitter.status.Status object at 0x7ff1f30cfa50>, <twitter.status.Status object at 0x7ff1f30cfc90>, <twitter.status.Status object at 0x7ff1f30cfed0>, <twitter.status.Status object at 0x7ff1f30cff90>, <twitter.status.Status object at 0x7ff1f30d6090>, <twitter.status.Status object at 0x7ff1f30d6210>, <twitter.status.Status object at 0x7ff1f30d6390>, <twitter.status.Status object at 0x7ff1f30d6590>, <twitter.status.Status object at 0x7ff1f30d66d0>, <twitter.status.Status object at 0x7ff1f30d6890>, <twitter.status.Status object at 0x7ff1f30d6990>, <twitter.status.Status object at 0x7ff1f30d6a10>, <twitter.status.Status object at 0x7ff1f30d6a90>, <twitter.status.Status object at 0x7ff1f30d6bd0>, <twitter.status.Status object at 0x7ff1f30d6c90>, <twitter.status.Status object at 0x7ff1f30d6e50>, <twitter.status.Status object at 0x7ff1f30dc050>, <twitter.status.Status object at 0x7ff1f30dc110>]
I had to dig their source code and it turns out you have to use AsDict()
json_results = [result.AsDict() for result in results]
such a pain...
I firmly recommend using tweepy instead of this lib for the lack of documentation.