I’m working on a Perl script to fetch and parse usernames from a Twitch chat API endpoint. The API returns JSON data with different user categories, but my current code only captures the first username instead of collecting all of them.
I want to gather all usernames into an array and then filter out the category labels like “moderators”, “vips”, “broadcasters”, etc.
Here’s my current attempt:
use strict;
use warnings;
my @username_list = fetch_chatters();
sub fetch_chatters {
my $api_url = "https://api.twitch.tv/helix/chat/chatters";
my $response = get($api_url);
my @parsed_data;
my $counter = 0;
while ($counter != 2){
my $username = (join "", grep(/"\s*(.*?)\s*"/, $response[$counter])) =~ /"\s*(.*?)\s*"/;
print $1;
$counter++;
}
return @parsed_data;
}
print @username_list;
What’s the best way to extract all usernames from this JSON response and store them properly in an array?