Elizabeth
Elizabeth

Reputation: 71

Convert json list into dictionary in python

The data I am using is Twitter API's twitter trending topics.

url_0 = 'https://api.twitter.com/1.1/trends/place.json?id=2459115'  
res = requests.get(url_0, auth=auth)  

print(res, res.status_code, res.headers['content-type'])  
print(res.url)

top_trends_twitter = res.json()  
data= top_trends_twitter[0]  

This is how data looks like:

[{'as_of': '2017-02-13T21:59:32Z',  
  'created_at': '2017-02-13T21:53:22Z',  
  'locations': [{'name': 'New York', 'woeid': 2459115}],  
  'trends': [{'name': 'Victor Cruz',  
    'promoted_content': None,  
    'query': '%22Victor+Cruz%22',  
    'tweet_volume': 45690,  
    'url': 'http://twitter.com/search?q=%22Victor+Cruz%22'},  
   {'name': '#percussion',  
    'promoted_content': None,  
    'query': '%23percussion',  
    'tweet_volume': None,  
    'url': 'http://twitter.com/search?q=%23percussion'},  .....etc

Now, after I connect the server with SQL, and create database and table, an error appears. This is the part that is causing me trouble:


for entry in data:  
    trendname = entry['trends']['name']  
    url = entry['trends']['url']  
    num_tweets = entry['trends']['trend_volume']  
    date= entry['as_of']  
    print("Inserting trend", trendname, "at", url)  
    query_parameters = (trendname, url, num_tweets, date)  
    cursor.execute(query_template, query_parameters) 



con.commit()  
cursor.close()  

Then, I get this error:


TypeError                                 Traceback (most recent call last)
<ipython-input-112-da3e17aadce0> in <module>()  
     29   
     30 for entry in data:  
---> 31     trendname = entry['trends']['name']  
     32     url = entry['trends']['url']  
     33     num_tweets = entry['trends']['trend_volume']  

TypeError: string indices must be integers

How can I get the set of strings into dictionary, so that I can use that for entry data code?

Upvotes: 0

Views: 2521

Answers (1)

Mohammad Yusuf
Mohammad Yusuf

Reputation: 17054

You Need entry['trends'][0]['name']. entry['trends'] is a list and you need integer index to access items of list.

Try like so:

data=[{'as_of': '2017-02-13T21:59:32Z',  
  'created_at': '2017-02-13T21:53:22Z',  
  'locations': [{'name': 'New York', 'woeid': 2459115}],  
  'trends': [{'name': 'Victor Cruz',  
    'promoted_content': None,  
    'query': '%22Victor+Cruz%22',  
    'tweet_volume': 45690,  
    'url': 'http://twitter.com/search?q=%22Victor+Cruz%22'},  
   {'name': '#percussion',  
    'promoted_content': None,  
    'query': '%23percussion',  
    'tweet_volume': None,  
    'url': 'http://twitter.com/search?q=%23percussion'}]}]

for entry in data:
    date= entry['as_of']
    for trend in entry['trends']:
        trendname = trend['name']  
        url = trend['url']  
        num_tweets = trend['tweet_volume']  
        print trendname, url, num_tweets, date

Output:

Victor Cruz http://twitter.com/search?q=%22Victor+Cruz%22 45690 2017-02-13T21:59:32Z
#percussion http://twitter.com/search?q=%23percussion None 2017-02-13T21:59:32Z

Upvotes: 2

Related Questions