0

I am attempting to print values from an API via JSON response. I was successful when I tried to print the first and foremost "live" value of the response, but I started running into problems when I tried printing anything other than the "live" value. Below is a sample of what I usually receive from the API, and my goal here is to print out only every visible "name" values.

{
   "live":[
      {
         "id":203003098,
         "yt_video_key":"K0uWjPoiMRY",
         "bb_video_id":"None",
         "title":"【Minecraft】Nature, Please Guide Me! ft. @Ceres Fauna Ch. hololive-EN   #holoCouncil",
         "thumbnail":"None",
         "status":"live",
         "live_schedule":"2021-09-14T02:00:00.000Z",
         "live_start":"2021-09-14T02:00:51.000Z",
         "live_end":"None",
         "live_viewers":11000,
         "channel":{
            "id":2260367,
            "yt_channel_id":"UC3n5uGu18FoCy23ggWWp8tA",
            "bb_space_id":"None",
            "name":"Nanashi Mumei Ch. hololive-EN",
            "photo":"https://yt3.ggpht.com/MI8E8Wfmc_ngNZXUwu8ad0D-OtqDhmqGVULEu25z-ccscwzJpAw-7ewFXzZYLK2jHB9d5OgQDq4=s800-c-k-c0x00ffffff-no-rj",
            "published_at":"2021-07-26T15:45:01.162Z",
            "twitter_link":"nanashimumei_en",
            "view_count":4045014,
            "subscriber_count":281000,
            "video_count":14
         }
      },
      {
         "id":202920144,
         "yt_video_key":"owk8w59Lcus",
         "bb_video_id":"None",
         "title":"【Undertale】平和なPルートでハッピーエンド目指す!【雪花ラミィ/ホロライブ】",
         "thumbnail":"None",
         "status":"live",
         "live_schedule":"2021-09-14T00:00:00.000Z",
         "live_start":"2021-09-14T00:04:22.000Z",
         "live_end":"None",
         "live_viewers":6200,
         "channel":{
            "id":31879,
            "yt_channel_id":"UCFKOVgVbGmX65RxO3EtH3iw",
            "bb_space_id":"None",
            "name":"Lamy Ch. 雪花ラミィ",
            "description":"ホロライブ所属。\n人里離れた白銀の大地に住む、雪の一族の令嬢。\nホロライブの笑顔や彩りあふれる配信に心を打たれ、\nお供のだいふくと共に家を飛び出した。\n真面目だが世間知らずで抜けたところがある。\n\n\n\nお問い合わせ\nカバー株式会社:http://cover-corp.com/ \n公式Twitter:https://twitter.com/hololivetv",
            "photo":"https://yt3.ggpht.com/ytc/AKedOLQDR06gp26jxNNXh88Hhv1o-pNrnlKrYruqUIOx=s800-c-k-c0x00ffffff-no-rj",
            "published_at":"2020-04-13T03:51:15.590Z",
            "twitter_link":"yukihanalamy",
            "view_count":66576847,
            "subscriber_count":813000,
            "video_count":430
         }
      },
      {
         "id":203019193,
         "yt_video_key":"QM2DjVNl1gY",
         "bb_video_id":"None",
         "title":"【MINECRAFT】 Adventuring with Mumei! #holoCouncil",
         "thumbnail":"None",
         "status":"live",
         "live_schedule":"2021-09-14T02:00:00.000Z",
         "live_start":"2021-09-14T02:00:58.000Z",
         "live_end":"None",
         "live_viewers":8600,
         "channel":{
            "id":2260365,
            "yt_channel_id":"UCO_aKKYxn4tvrqPjcTzZ6EQ",
            "bb_space_id":"None",
            "name":"Ceres Fauna Ch. hololive-EN",
            "description":"A member of the Council and the Keeper of \"Nature,\" the second concept created by the Gods.\nShe has materialized in the mortal realm as a druid in a bid to save nature.\nShe has Kirin blood flowing in her veins, and horns that are made out of the branches of a certain tree; they are NOT deer antlers.\n\n\"Nature\" refers to all organic matter on the planet except mankind.\nIt is long said that her whispers, as an avatar of Mother Nature, have healing properties. Whether or not that is true is something only those who have heard them can say.\nWhile she is usually affable, warm, and slightly mischievous, any who anger her will bear the full brunt of Nature\\'s fury.\n\n",
            "photo":"https://yt3.ggpht.com/0lkccaVapSr1Z3uuXWbnaQxeqRWr9Tcs4R9rLBRSrAsN9gLacpiT2OFWfFKr4NhF97_hqK3eTg=s800-c-k-c0x00ffffff-no-rj",
            "published_at":"2021-07-26T15:38:58.797Z",
            "twitter_link":"ceresfauna",
            "view_count":5003954,
            "subscriber_count":253000,
            "video_count":17
         }
      }
   ],

My code:

url = "https://api.holotools.app/v1/live"

    response = urlopen(url)

    data_json = json.loads(response.read())

    print(data_json['live'])
Ryley
  • 13
  • 2

1 Answers1

0

I think you're new to programming language so following is the special note for the new programmer.

You did well in printing the data but this is not end because your goal is to get the name so you need to traverse in the response one by one let me show you

url = "https://api.holotools.app/v1/live"
response = urlopen(url)
data_json = json.loads(response.read())
dicts = data_json['live']

#Why I'm using loop here? Because we need to get every element of list(data_json['live'] is a list)
for dict in dicts:
    print(dict["channel"]["name"]   
    ***Now here after getting single element from list as a dict I select its key which is "channel"***

Following are some useful links through which you can learn how to traverse in json

  1. https://www.kite.com/python/answers/how-to-iterate-through-a-json-string-in-python

  2. https://www.delftstack.com/howto/python/iterate-through-json-python/

There are also stackoverflow answer which are about: How to get data from json? but it need some programming skills too following is the link of answers.

  1. Iterating through a JSON object
  2. Looping through a JSON array in Python
  3. How can I loop over entries in JSON?
Adam Strauss
  • 1,889
  • 2
  • 15
  • 45