Search Tweets with Python API : Error "No JSON object could be decoded"


#1

Hi,

I’m using Python to analyse tweets.
I always get an error when trying to search tweets about a word or an hastag.
Here is a draf of the error :

ValueError                                Traceback (most recent call last)
<ipython-input-23-c59f6b4c0eda> in <module>()
      2 
      3 q = '#barcampcameroon'
----> 4 results = twitter_search(twitter_api, q, max_results = 1000)
      5 
      6 save_json(q, results)

<ipython-input-22-756dc1f1854b> in twitter_search(twitter_api, q, max_results, **kw)
     21 def twitter_search(twitter_api, q, max_results = 200, **kw) :
     22 
---> 23     search_results = twitter_api.search.tweets(q=q , count = 75, **kw)
     24 
     25     statuses = search_results['statuses']

/usr/local/lib/python2.7/dist-packages/twitter/api.pyc in __call__(self, **kwargs)
    264 
    265         req = urllib_request.Request(uriBase, body, headers)
--> 266         return self._handle_response(req, uri, arg_data, _timeout)
    267 
    268     def _handle_response(self, req, uri, arg_data, _timeout=None):

/usr/local/lib/python2.7/dist-packages/twitter/api.pyc in _handle_response(self, req, uri, arg_data, _timeout)
    288                 return wrap_response({}, handle.headers)
    289             elif "json" == self.format:
--> 290                 res = json.loads(data.decode('utf8'))
    291                 return wrap_response(res, handle.headers)
    292             else:

/usr/lib/python2.7/json/__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    336             parse_int is None and parse_float is None and
    337             parse_constant is None and object_pairs_hook is None and not kw):
--> 338         return _default_decoder.decode(s)
    339     if cls is None:
    340         cls = JSONDecoder

/usr/lib/python2.7/json/decoder.pyc in decode(self, s, _w)
    364 
    365         """
--> 366         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    367         end = _w(s, end).end()
    368         if end != len(s):

/usr/lib/python2.7/json/decoder.pyc in raw_decode(self, s, idx)
    380         """
    381         try:
--> 382             obj, end = self.scan_once(s, idx)
    383         except StopIteration:
    384             raise ValueError("No JSON object could be decoded")

ValueError: Unterminated string starting at: line 1 column 81399 (char 81398)

It’s always about No JSON object could be decode
Any idea about to solve it please ?


#2

Do you know what the response you are getting looks like - i.e. what is the raw JSON data? does it look valid before you pass it to the JSON decoder?


#3

Am facing the same issue.


#4

Can you provide the raw JSON response you are getting before passing it to the decoder?