I am trying to accomplish the same goal @Jannik was here: Python: Getting all values of a specific key from json
I'm attempting to use the same solution by I don't know what to put in the result['']
because my results aren't labeled with a parent title like 'ABC'
. How can I pull all the values for the same key listed throughout my JSON data?
Here's my JSON 'list':
result = [
{
"created_at":"Wed Oct 13 19:01:18 +0000 2021",
"id":1448363231366590471,
"id_str":"1448363231366590471",
"text":"The editor of @SpaceRef and @NASAWatch - @KeithCowing - will be live on @DeutscheWelle to talk about the… https://example.com",
"truncated":true,
"entities":{
"hashtags":[],
"symbols":[],
"user_mentions":[
{
"screen_name":"SpaceRef",
"name":"SpaceRef",
"id":8623332,
"id_str":"8623332",
"indices":[]
}
]
"retweet_count": 1,
"favorite_count": 3
}
]
I am attempting to pull all instances of keys 'retweet_count' and 'favorite_count' (not shown in JSON data) and their corresponding values.
The following results in the TypeError: list indices must be integers or slices, not str
:
def get_val(key):
return [entry[key] for entry in result[''].values()]
print(get_val('retweet_count'))
Here's how I've accomplished getting them one-by-one, by it's not practical for the amount I need:
result = requests.get(url,headers={'Content-Type':'application/json','Authorization': 'Bearer {}'.format(bearer_token)}).json()
retweets1 = result[0]['retweet_count']
likes1 = result[0]['favorite_count']
retweets2 = result[1]['retweet_count']
likes2 = result[1]['favorite_count']
retweets3 = result[2]['retweet_count']
likes3 = result[2]['favorite_count']
retweets4 = result[3]['retweet_count']
likes4 = result[3]['favorite_count']
etc...
print(retweets1, retweets2, etc...)