Search code examples
javascriptjsonnode.jstwitch

Parsing twitchapi data


Ive just started coding a twitch bot and im looking at creating a point system but to do this i need to see the active viewers. To do this i need to parse the viewers through the twitch api. Twitch gives you a json file but i dont really know how to parse it. It would be great if someone could show me how to create an array of the "viewers"

Heres a sample of it :

{
  "_links": {},
  "chatter_count": 3,
  "chatters": {
    "moderators": [
      "kong_plays",
      "nightbot"
    ],
    "staff": [],
    "admins": [],
    "global_mods": [],
    "viewers": [
      "kingnosebleed"
    ]
  }
}

Thanks in advance

The link i use to get my json file for my channel is: https://tmi.twitch.tv/group/user/kong_plays/chatters


Solution

  • Fetching the JSON from a URL is easy, look at this answer. As for parsing the JSON, if you're trying to get the array under the viewers key, you can do:

    var viewers = json.chatters.viewers

    This should return the viewers array.