Search code examples
javascriptjsontwitch

Javascript Twitch API Json Data


The json data is here

{


 "streams": [
    {
      "_id": 22748053072,
      "game": "Overwatch",
      "viewers": 11344,
      "created_at": "2016-08-10T00:04:34Z",
      "video_height": 720,
      "average_fps": 60.3225806452,
      "delay": 0,
      "is_playlist": false,
      "_links": {
        "self": "https:\/\/api.twitch.tv\/kraken\/streams\/timthetatman"
      },
      "preview": {
        "small": "https:\/\/static-cdn.jtvnw.net\/previews-ttv\/live_user_timthetatman-80x45.jpg",
        "medium": "https:\/\/static-cdn.jtvnw.net\/previews-ttv\/live_user_timthetatman-320x180.jpg",
        "large": "https:\/\/static-cdn.jtvnw.net\/previews-ttv\/live_user_timthetatman-640x360.jpg",
        "template": "https:\/\/static-cdn.jtvnw.net\/previews-ttv\/live_user_timthetatman-{width}x{height}.jpg"
      },
      "channel": {
        "mature": true,
        "status": "gold genji tonight? no strums tmrw- bach party all day!: dbh.la\/timthetatman",
        "broadcaster_language": "en",
        "display_name": "TimTheTatman",
        "game": "Overwatch",
        "language": "en",
        "_id": 36769016,
        "name": "timthetatman",
        "created_at": "2012-10-08T18:13:50Z",
        "updated_at": "2016-08-10T03:33:20Z",
        "delay": null,
        "logo": "https:\/\/static-cdn.jtvnw.net\/jtv_user_pictures\/timthetatman-profile_image-35bdf9d7b751789e-300x300.jpeg",
        "banner": null,
        "video_banner": "https:\/\/static-cdn.jtvnw.net\/jtv_user_pictures\/timthetatman-channel_offline_image-f4e88d4e35841793-1920x1080.png",
        "background": null,
        "profile_banner": null,
        "profile_banner_background_color": null,
        "partner": true,
        "url": "https:\/\/www.twitch.tv\/timthetatman",
        "views": 27956760,
        "followers": 743104,
        "_links": {
          "self": "https:\/\/api.twitch.tv\/kraken\/channels\/timthetatman",
          "follows": "https:\/\/api.twitch.tv\/kraken\/channels\/timthetatman\/follows",
          "commercial": "https:\/\/api.twitch.tv\/kraken\/channels\/timthetatman\/commercial",
          "stream_key": "https:\/\/api.twitch.tv\/kraken\/channels\/timthetatman\/stream_key",
          "chat": "https:\/\/api.twitch.tv\/kraken\/chat\/timthetatman",
          "features": "https:\/\/api.twitch.tv\/kraken\/channels\/timthetatman\/features",
          "subscriptions": "https:\/\/api.twitch.tv\/kraken\/channels\/timthetatman\/subscriptions",
          "editors": "https:\/\/api.twitch.tv\/kraken\/channels\/timthetatman\/editors",
          "teams": "https:\/\/api.twitch.tv\/kraken\/channels\/timthetatman\/teams",
          "videos": "https:\/\/api.twitch.tv\/kraken\/channels\/timthetatman\/videos"
        }
      }
    }
  ],
  "_total": 1,
  "_links": {
    "self": "https:\/\/api.twitch.tv\/kraken\/streams?channel=timthetatman&limit=25&offset=0",
    "next": "https:\/\/api.twitch.tv\/kraken\/streams?channel=timthetatman&limit=25&offset=25",
    "featured": "https:\/\/api.twitch.tv\/kraken\/streams\/featured",
    "summary": "https:\/\/api.twitch.tv\/kraken\/streams\/summary",
    "followed": "https:\/\/api.twitch.tv\/kraken\/streams\/followed"
  }
}

When I convert that to an object and do something like "streamData.streams.game" it gives me an error, and when I look in a json viewer it seems like streams is an an array, because it has a 0 by it, however I'm not sure how I can select the right one. I'm so stuck on this, I have no idea how to select any values in streams.


Solution

  • You are correct, the streams field is an array.

    To access elements of an array, you simply do arrayName[elementNumber]. In the example you provided, the array only has 1 entry, so you can simply do streamData.streams[0].game. That will select the first (and only) element in the streams array and return it's game field.