gujaratiraja
gujaratiraja

Reputation: 379

Extract a single value from json array

I am writing a program to show location based trending topics from twitter. I am able to get the json response from twitter and also converting it to a dictionary using json.loads()

I am not able to extract a single value from the dictonary.

Json response is:

[
  {
    "trends": [
      {
        "name": "#UnrivalledVerithanamRecords",
        "url": "http://twitter.com/search?q=%23UnrivalledVerithanamRecords",
        "promoted_content": null,
        "query": "%23UnrivalledVerithanamRecords",
        "tweet_volume": 383837
      },
      {
        "name": "#justiceformadhav",
        "url": "http://twitter.com/search?q=%23justiceformadhav",
        "promoted_content": null,
        "query": "%23justiceformadhav",
        "tweet_volume": null
      },
      {
        "name": "#WaitingFor_T_H_A_L_A_60",
        "url": "http://twitter.com/search?q=%23WaitingFor_T_H_A_L_A_60",
        "promoted_content": null,
        "query": "%23WaitingFor_T_H_A_L_A_60",
        "tweet_volume": 403385
      },
      {
        "name": "#Vanitha",
        "url": "http://twitter.com/search?q=%23Vanitha",
        "promoted_content": null,
        "query": "%23Vanitha",
        "tweet_volume": null
      },
      {
        "name": "#OpenTheDoorForKavin",
        "url": "http://twitter.com/search?q=%23OpenTheDoorForKavin",
        "promoted_content": null,
        "query": "%23OpenTheDoorForKavin",
        "tweet_volume": 22660
      },
      {
        "name": "world test championship",
        "url": "http://twitter.com/search?q=%22world+test+championship%22",
        "promoted_content": null,
        "query": "%22world+test+championship%22",
        "tweet_volume": null
      },
      {
        "name": "subscribed",
        "url": "http://twitter.com/search?q=subscribed",
        "promoted_content": null,
        "query": "subscribed",
        "tweet_volume": null
      },
      {
        "name": "Navas",
        "url": "http://twitter.com/search?q=Navas",
        "promoted_content": null,
        "query": "Navas",
        "tweet_volume": 101256
      },
      {
        "name": "Icardi",
        "url": "http://twitter.com/search?q=Icardi",
        "promoted_content": null,
        "query": "Icardi",
        "tweet_volume": 196170
      },
      {
        "name": "\u0917\u0923\u092a\u0924\u093f \u092c\u092a\u094d\u092a\u093e",
        "url": "http://twitter.com/search?q=%22%E0%A4%97%E0%A4%A3%E0%A4%AA%E0%A4%A4%E0%A4%BF+%E0%A4%AC%E0%A4%AA%E0%A5%8D%E0%A4%AA%E0%A4%BE%22",
        "promoted_content": null,
        "query": "%22%E0%A4%97%E0%A4%A3%E0%A4%AA%E0%A4%A4%E0%A4%BF+%E0%A4%AC%E0%A4%AA%E0%A5%8D%E0%A4%AA%E0%A4%BE%22",
        "tweet_volume": 51477
      },
      {
        "name": "#EconomyKhatreMeinHai",
        "url": "http://twitter.com/search?q=%23EconomyKhatreMeinHai",
        "promoted_content": null,
        "query": "%23EconomyKhatreMeinHai",
        "tweet_volume": 18215
      },
      {
        "name": "#VoteForKavin",
        "url": "http://twitter.com/search?q=%23VoteForKavin",
        "promoted_content": null,
        "query": "%23VoteForKavin",
        "tweet_volume": 35500
      },
      {
        "name": "#mohammedshami",
        "url": "http://twitter.com/search?q=%23mohammedshami",
        "promoted_content": null,
        "query": "%23mohammedshami",
        "tweet_volume": null
      },
      {
        "name": "#PakTorturesMinorities",
        "url": "http://twitter.com/search?q=%23PakTorturesMinorities",
        "promoted_content": null,
        "query": "%23PakTorturesMinorities",
        "tweet_volume": null
      },
      {
        "name": "#NammaBengaluru",
        "url": "http://twitter.com/search?q=%23NammaBengaluru",
        "promoted_content": null,
        "query": "%23NammaBengaluru",
        "tweet_volume": null
      },
      {
        "name": "#congaccusesnonmuslims",
        "url": "http://twitter.com/search?q=%23congaccusesnonmuslims",
        "promoted_content": null,
        "query": "%23congaccusesnonmuslims",
        "tweet_volume": null
      },
      {
        "name": "#PawanJaiswal",
        "url": "http://twitter.com/search?q=%23PawanJaiswal",
        "promoted_content": null,
        "query": "%23PawanJaiswal",
        "tweet_volume": null
      },
      {
        "name": "#MudinjaOpenPandraDoorah",
        "url": "http://twitter.com/search?q=%23MudinjaOpenPandraDoorah",
        "promoted_content": null,
        "query": "%23MudinjaOpenPandraDoorah",
        "tweet_volume": 37035
      },
      {
        "name": "#ZomatoExposed",
        "url": "http://twitter.com/search?q=%23ZomatoExposed",
        "promoted_content": null,
        "query": "%23ZomatoExposed",
        "tweet_volume": null
      },
      {
        "name": "#KingKohli",
        "url": "http://twitter.com/search?q=%23KingKohli",
        "promoted_content": null,
        "query": "%23KingKohli",
        "tweet_volume": null
      },
      {
        "name": "#JohnWick",
        "url": "http://twitter.com/search?q=%23JohnWick",
        "promoted_content": null,
        "query": "%23JohnWick",
        "tweet_volume": null
      },
      {
        "name": "#GaneshChaturthi",
        "url": "http://twitter.com/search?q=%23GaneshChaturthi",
        "promoted_content": null,
        "query": "%23GaneshChaturthi",
        "tweet_volume": 254529
      },
      {
        "name": "#HappyBirthdayPawanKalyan",
        "url": "http://twitter.com/search?q=%23HappyBirthdayPawanKalyan",
        "promoted_content": null,
        "query": "%23HappyBirthdayPawanKalyan",
        "tweet_volume": 4559770
      },
      {
        "name": "#\u0917\u0923\u0947\u0936_\u091a\u0924\u0941\u0930\u094d\u0925\u0940",
        "url": "http://twitter.com/search?q=%23%E0%A4%97%E0%A4%A3%E0%A5%87%E0%A4%B6_%E0%A4%9A%E0%A4%A4%E0%A5%81%E0%A4%B0%E0%A5%8D%E0%A4%A5%E0%A5%80",
        "promoted_content": null,
        "query": "%23%E0%A4%97%E0%A4%A3%E0%A5%87%E0%A4%B6_%E0%A4%9A%E0%A4%A4%E0%A5%81%E0%A4%B0%E0%A5%8D%E0%A4%A5%E0%A5%80",
        "tweet_volume": 52427
      },
      {
        "name": "#happybirthdaypowerstar",
        "url": "http://twitter.com/search?q=%23happybirthdaypowerstar",
        "promoted_content": null,
        "query": "%23happybirthdaypowerstar",
        "tweet_volume": 202196
      },
      {
        "name": "#MondayMotivation",
        "url": "http://twitter.com/search?q=%23MondayMotivation",
        "promoted_content": null,
        "query": "%23MondayMotivation",
        "tweet_volume": 122241
      },
      {
        "name": "#AbhinandanVarthaman",
        "url": "http://twitter.com/search?q=%23AbhinandanVarthaman",
        "promoted_content": null,
        "query": "%23AbhinandanVarthaman",
        "tweet_volume": 17277
      },
      {
        "name": "#RomilaThapar",
        "url": "http://twitter.com/search?q=%23RomilaThapar",
        "promoted_content": null,
        "query": "%23RomilaThapar",
        "tweet_volume": null
      },
      {
        "name": "#VinayagarChaturthi",
        "url": "http://twitter.com/search?q=%23VinayagarChaturthi",
        "promoted_content": null,
        "query": "%23VinayagarChaturthi",
        "tweet_volume": 19151
      },
      {
        "name": "#kulbhushanjadhav",
        "url": "http://twitter.com/search?q=%23kulbhushanjadhav",
        "promoted_content": null,
        "query": "%23kulbhushanjadhav",
        "tweet_volume": null
      },
      {
        "name": "#BoycottRedLabel",
        "url": "http://twitter.com/search?q=%23BoycottRedLabel",
        "promoted_content": null,
        "query": "%23BoycottRedLabel",
        "tweet_volume": null
      },
      {
        "name": "#mondaythoughts",
        "url": "http://twitter.com/search?q=%23mondaythoughts",
        "promoted_content": null,
        "query": "%23mondaythoughts",
        "tweet_volume": 41792
      },
      {
        "name": "#\u0bb5\u0bbf\u0ba8\u0bbe\u0baf\u0b95\u0bb0\u0bcd\u0b9a\u0ba4\u0bc1\u0bb0\u0bcd\u0ba4\u0bcd\u0ba4\u0bbf",
        "url": "http://twitter.com/search?q=%23%E0%AE%B5%E0%AE%BF%E0%AE%A8%E0%AE%BE%E0%AE%AF%E0%AE%95%E0%AE%B0%E0%AF%8D%E0%AE%9A%E0%AE%A4%E0%AF%81%E0%AE%B0%E0%AF%8D%E0%AE%A4%E0%AF%8D%E0%AE%A4%E0%AE%BF",
        "promoted_content": null,
        "query": "%23%E0%AE%B5%E0%AE%BF%E0%AE%A8%E0%AE%BE%E0%AE%AF%E0%AE%95%E0%AE%B0%E0%AF%8D%E0%AE%9A%E0%AE%A4%E0%AF%81%E0%AE%B0%E0%AF%8D%E0%AE%A4%E0%AF%8D%E0%AE%A4%E0%AE%BF",
        "tweet_volume": null
      },
      {
        "name": "#HappyBirthdayKichchaSudeep",
        "url": "http://twitter.com/search?q=%23HappyBirthdayKichchaSudeep",
        "promoted_content": null,
        "query": "%23HappyBirthdayKichchaSudeep",
        "tweet_volume": 27395
      },
      {
        "name": "#herofirstlook",
        "url": "http://twitter.com/search?q=%23herofirstlook",
        "promoted_content": null,
        "query": "%23herofirstlook",
        "tweet_volume": 60975
      },
      {
        "name": "#28DaysOfKashmirShutdown",
        "url": "http://twitter.com/search?q=%2328DaysOfKashmirShutdown",
        "promoted_content": null,
        "query": "%2328DaysOfKashmirShutdown",
        "tweet_volume": 41193
      },
      {
        "name": "#matkaroforward",
        "url": "http://twitter.com/search?q=%23matkaroforward",
        "promoted_content": null,
        "query": "%23matkaroforward",
        "tweet_volume": null
      },
      {
        "name": "#Mirzapur",
        "url": "http://twitter.com/search?q=%23Mirzapur",
        "promoted_content": null,
        "query": "%23Mirzapur",
        "tweet_volume": 11950
      },
      {
        "name": "#Chandrayaan2",
        "url": "http://twitter.com/search?q=%23Chandrayaan2",
        "promoted_content": null,
        "query": "%23Chandrayaan2",
        "tweet_volume": 25911
      },
      {
        "name": "#ARSTOT",
        "url": "http://twitter.com/search?q=%23ARSTOT",
        "promoted_content": null,
        "query": "%23ARSTOT",
        "tweet_volume": 19960
      },
      {
        "name": "#mahapackageforpuri",
        "url": "http://twitter.com/search?q=%23mahapackageforpuri",
        "promoted_content": null,
        "query": "%23mahapackageforpuri",
        "tweet_volume": null
      },
      {
        "name": "#GodMorningMonday",
        "url": "http://twitter.com/search?q=%23GodMorningMonday",
        "promoted_content": null,
        "query": "%23GodMorningMonday",
        "tweet_volume": 64217
      },
      {
        "name": "#INXMediaCase",
        "url": "http://twitter.com/search?q=%23INXMediaCase",
        "promoted_content": null,
        "query": "%23INXMediaCase",
        "tweet_volume": null
      },
      {
        "name": "#ENPTWeekBegins",
        "url": "http://twitter.com/search?q=%23ENPTWeekBegins",
        "promoted_content": null,
        "query": "%23ENPTWeekBegins",
        "tweet_volume": 53345
      },
      {
        "name": "#ak60\u0ca4\u0cb2\u0cbe60\u0985\u099c\u09bf\u09a460\u0d85\u0da2\u0dd2\u0dad\u0dca60",
        "url": "http://twitter.com/search?q=%23ak60%E0%B2%A4%E0%B2%B2%E0%B2%BE60%E0%A6%85%E0%A6%9C%E0%A6%BF%E0%A6%A460%E0%B6%85%E0%B6%A2%E0%B7%92%E0%B6%AD%E0%B7%8A60",
        "promoted_content": null,
        "query": "%23ak60%E0%B2%A4%E0%B2%B2%E0%B2%BE60%E0%A6%85%E0%A6%9C%E0%A6%BF%E0%A6%A460%E0%B6%85%E0%B6%A2%E0%B7%92%E0%B6%AD%E0%B7%8A60",
        "tweet_volume": 25604
      },
      {
        "name": "#\u0917\u0923\u092a\u0924\u093f_\u092c\u092a\u094d\u092a\u093e_\u092e\u094b\u0930\u092f\u093e",
        "url": "http://twitter.com/search?q=%23%E0%A4%97%E0%A4%A3%E0%A4%AA%E0%A4%A4%E0%A4%BF_%E0%A4%AC%E0%A4%AA%E0%A5%8D%E0%A4%AA%E0%A4%BE_%E0%A4%AE%E0%A5%8B%E0%A4%B0%E0%A4%AF%E0%A4%BE",
        "promoted_content": null,
        "query": "%23%E0%A4%97%E0%A4%A3%E0%A4%AA%E0%A4%A4%E0%A4%BF_%E0%A4%AC%E0%A4%AA%E0%A5%8D%E0%A4%AA%E0%A4%BE_%E0%A4%AE%E0%A5%8B%E0%A4%B0%E0%A4%AF%E0%A4%BE",
        "tweet_volume": null
      },
      {
        "name": "#vikramlander",
        "url": "http://twitter.com/search?q=%23vikramlander",
        "promoted_content": null,
        "query": "%23vikramlander",
        "tweet_volume": null
      },
      {
        "name": "#ManmohanSingh",
        "url": "http://twitter.com/search?q=%23ManmohanSingh",
        "promoted_content": null,
        "query": "%23ManmohanSingh",
        "tweet_volume": null
      },
      {
        "name": "#drsinghoneconomiccrisis",
        "url": "http://twitter.com/search?q=%23drsinghoneconomiccrisis",
        "promoted_content": null,
        "query": "%23drsinghoneconomiccrisis",
        "tweet_volume": null
      },
      {
        "name": "#happybirthdayjungkook",
        "url": "http://twitter.com/search?q=%23happybirthdayjungkook",
        "promoted_content": null,
        "query": "%23happybirthdayjungkook",
        "tweet_volume": 119143
      }
    ],
    "as_of": "2019-09-02T22:02:12Z",
    "created_at": "2019-09-02T21:57:15Z",
    "locations": [
      {
        "name": "Ahmedabad",
        "woeid": 2295402
      }
    ]
  }
]

My code is

CONSUMER_KEY = 'xxx'
CONSUMER_SECRET = 'xxx'
ACCESS_KEY = 'xxx-xxx'
ACCESS_SECRET = 'xxx'


auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
tweepyapi = tweepy.API(auth)

print(json.dumps(tweepyapi.trends_place(2295402)))
my_dict = json.loads(json.dumps(tweepyapi.trends_place(2295402), indent=2))
print(my_dict['trends']['name'])

I get the following error: TypeError: list indices must be integers or slices, not str

how do i extract only "names" from the response?

Upvotes: 0

Views: 452

Answers (2)

Trenton McKinney
Trenton McKinney

Reputation: 62373

I would use pandas, because it makes accessing the data easier.

  • If my_dict is loaded into a pandas.DataFrame, all of the information will be easier to access and extract business intelligence from.
  • my_dict[0]["trends"][0]["name"] this type of indexing to find a simple piece of information will not be necessary

Create a DataFrame:

import pandas as pd
from pandas.io.json import json_normalize

df = df = json_normalize(my_dict, record_path='trends')

Output of df:

                         name                                                         url  promoted_content                           query  tweet_volume
 #UnrivalledVerithanamRecords  http://twitter.com/search?q=%23UnrivalledVerithanamRecords               NaN  %23UnrivalledVerithanamRecords      383837.0
            #justiceformadhav             http://twitter.com/search?q=%23justiceformadhav               NaN             %23justiceformadhav           NaN
     #WaitingFor_T_H_A_L_A_60      http://twitter.com/search?q=%23WaitingFor_T_H_A_L_A_60               NaN      %23WaitingFor_T_H_A_L_A_60      403385.0
                     #Vanitha                      http://twitter.com/search?q=%23Vanitha               NaN                      %23Vanitha           NaN
         #OpenTheDoorForKavin          http://twitter.com/search?q=%23OpenTheDoorForKavin               NaN          %23OpenTheDoorForKavin       22660.0

Now it's easier to access any data:

return name:

df.name.head()  # return top 5 names for example

0    #UnrivalledVerithanamRecords
1               #justiceformadhav
2        #WaitingFor_T_H_A_L_A_60
3                        #Vanitha
4            #OpenTheDoorForKavin
Name: name, dtype: object

return tweet_volume > some number:

df[df.tweet_volume > 300_000]

                         name                                                         url  promoted_content                           query  tweet_volume
 #UnrivalledVerithanamRecords  http://twitter.com/search?q=%23UnrivalledVerithanamRecords               NaN  %23UnrivalledVerithanamRecords      383837.0
     #WaitingFor_T_H_A_L_A_60      http://twitter.com/search?q=%23WaitingFor_T_H_A_L_A_60               NaN      %23WaitingFor_T_H_A_L_A_60      403385.0
    #HappyBirthdayPawanKalyan     http://twitter.com/search?q=%23HappyBirthdayPawanKalyan               NaN     %23HappyBirthdayPawanKalyan     4559770.0

Upvotes: 0

Telmo Trooper
Telmo Trooper

Reputation: 5674

The problem is that you have a list of objects (notice how your JSON starts with brackets), so you have to access the proper indices to get your information. Example:

print(my_dict[0]["trends"][0]["name"])

This should get the "trends" from the first entry and then the name for the first trend inside that.

Upvotes: 1

Related Questions