Streaming API statuses/filter.json?track not delivering JSON python


Hello, I am outputing a Twitter stream (“”) to a file using Python. However, the file is outputting an “instance” instead of a “json” (see relevant part in bold):

import oauth2 as oauth
import urllib2 as urllib
import json

# Filled correctly, no authentication problem
api_key = "XXX"
api_secret = "XXX"
access_token_key = "XXX"
access_token_secret = "XXX"

_debug = 0

oauth_token    = oauth.Token(key=access_token_key, secret=access_token_secret)
oauth_consumer = oauth.Consumer(key=api_key, secret=api_secret)

signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()

http_method = "GET"

http_handler  = urllib.HTTPHandler(debuglevel=_debug)
https_handler = urllib.HTTPSHandler(debuglevel=_debug)

Construct, sign, and open a twitter request
using the hard-coded credentials above.
def twitterreq(url, method, parameters):
  req = oauth.Request.from_consumer_and_token(oauth_consumer,

  req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)

  headers = req.to_header()

  if http_method == "POST":
    encoded_post_data = req.to_postdata()
    encoded_post_data = None
    url = req.to_url()

  opener = urllib.OpenerDirector()

  response =, encoded_post_data)

  return response

**def fetchcontinuousstream():
  # For streaming of tweets use
  url = ""
  parameters = []
  response = twitterreq(url, "GET", parameters)
  print "Type of the response"
  print type(response)
  for line in response:
    print type(line)**

**if __name__ == '__main__':

The result is this:

Type of the response
<type ‘instance’>
<type ‘str’>

So basically the response is an instance, not a json/dict, each line is just a string…

How can I obtain a JSON instead?


if I try with this code:

response = json.loads(twitterreq(url, "GET", parameters))

it doesn’t work:

File "", line 64, in fetchcontinuousstream response = json.loads(twitterreq(url, "GET", parameters)) File "/usr/lib/python2.7/json/", line 326, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer

Also loads() at line level doesn’t work either:

for line in response: 
  tweet = json.loads(line) 

It doesn’t work either:

File "", line 67, in fetchcontinuousstream tweet = json.loads(line) File "/usr/lib/python2.7/json/", line 326, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.7/json/", line 384, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded


It’s been a while since I’ve used urllib’s opener, but try this:



You’re nearly there. Try

for line in response:
  if line != "":
    tweet= json.loads(line)
    print tweet["text"]


That did it, thanks a lot Isaach!