user3063864
user3063864

Reputation: 721

Python - Having trouble creating a dictionary from a JSON

I'm trying to take the JSON from a twitter get_user query and turn it into a Python object that I can extract data from (twitter handle, location, screen name, etc.)

Here is what I created. I am not sure why it doesn't work.

api = tweepy.API(auth,parser=tweepy.parsers.JSONParser())

user = api.search_users('google.com')

t_dict = json.loads(user)

pprint(t_dict)

Error:

Traceback (most recent call last):
  File "Get_User_By_URL.py", line 23, in <module>
    t_dict = json.loads(user)
  File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

Upvotes: 1

Views: 125

Answers (1)

JuniorCompressor
JuniorCompressor

Reputation: 20025

api.search_users is already returning a python object. It isn't a json string that needs to be parsed. According to tweetpy documentation search_users actually returns a list of users. So the following is possible:

for user in api.search_users('google.com'):
    print user.screen_name 

Upvotes: 1

Related Questions