Miyako has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am having a slight issue in Perl 5.2. I've used libjson-perl for a few years and written many things which used it to decode Json from websites. I am now in the process of writing a bot for the chat app Discord which uses a Json api, much like other places I've written bots for previously. I know my code is right, or at least should be, yet I am getting the "not a hash reference error." Here is the output I am getting in my terminal.

$VAR1 = [ { 'mentions' => [], 'author' => { 'avatar' => '035a793a88596c440cb0434f272d8f3 +c', 'discriminator' => '4559', 'id' => '159495601208164353', 'username' => 'MiyakoProductions' }, 'embeds' => [], 'channel_id' => '193239098226376705', 'tts' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' ), 'mention_roles' => [], 'mention_everyone' => $VAR1->[0]{'tts'}, 'content' => 'so i wrote a simple script to pull each one +automatically', 'id' => '233412710220562432', 'type' => 0, 'pinned' => $VAR1->[0]{'tts'}, 'edited_timestamp' => undef, 'timestamp' => '2016-10-06T02:18:46.715000+00:00', 'attachments' => [] } ]; Not a HASH reference at discord.pl line 70.

My code itself is:

sub getMessages($) { my $apiUrl = $baseUrl . "channels/" . $_[0] . "/messages?limit=1"; print "$apiUrl\n"; my $discordClient = WWW::Mechanize->new( agent => 'DiscordBot (Lib +Discord-Perl5 1)' ); $discordClient->add_header('Authorization' => "$authToken"); my $clientConnected = eval { $discordClient->get($apiUrl, 'Content +-Type' => 'application/json'); }; if (! $clientConnected ) { errorCon(); } else { # Set values when connection is successful our $apiContent = $discordClient->content; #print Dumper($apiContent) . "\n"; my @apiJson = decode_json( $apiContent ); print "Pretty Response:\n" . Dumper( @apiJson ) . "\n"; my $msgID = $apiJson[0]->{'id'}; my $msgAuthor = $apiJson[0]->{'author'}->{'username'}; my $msgContent = $apiJson[0]->{'content'}; if ($msgID > $lastMsgID) { print "<$msgAuthor> $msgContent\n"; $lastMsgID = $msgID; } } } sub errorCon { print "Error: Unable to connect to Discord!\n"; }

The response is generally 50 messages long, however I limit it to 1 message for ease of parsing and debugging. Therefore, I am treating the response first as an array, and yes I have tried to foreach through it. After treating it as an array I do the normal syntax to retrieve the value of a key. Is there something I am missing?

Replies are listed 'Best First'.
Re: Perplexing JSON decoding issue.
by haukex (Archbishop) on Oct 06, 2016 at 09:24 UTC

    Hi Miyako,

    I'll take a guess: If your JSON string looks like "[ ... ]", then decode_json should return an array reference, not an array as you seem to want with my @apiJson = decode_json( $apiContent );. Instead, you'll have to dereference that array reference with something like my $apiJson = decode_json( $apiContent ); my $msgID = $apiJson->[0]->{'id'};

    Hope this helps,
    -- Hauke D

Re: Perplexing JSON decoding issue.
by choroba (Cardinal) on Oct 06, 2016 at 09:23 UTC
    Hallo Miyako, welcome to the Monastery!

    Your sample code doesn't have enough lines to die at line 70. Is Dumper the one found in Data::Dumper? If so, you should give it a reference to the variable you want it to print - otherwise, it prints the elements of the array, which means $apiJson[0] is an array reference, and $apiJson[0][0] is the hash reference you want to use. Or, don't use an array at all:

    my $apiJson = decode_json( $apiContent ); print "Pretty Response:\n", Dumper( $apiJson ); my $msgID = $apiJson->[0]{id};

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Perplexing JSON decoding issue.
by Miyako (Initiate) on Oct 06, 2016 at 17:27 UTC

    @choroba, it is the Dumper from Data::Dumper; I use it often when working with Json to see the entire response formatted nicely. Helps a lot to see what variables and arrays are listed.

    @haukex, that worked to fix it perfectly. Thanks for that. I'd have torn my hair out all day trying to figure out why it wasn't working when every other time I used Json it worked. I also noticed that it worked as $apiJson[0]->[0]->{'id'}; Not entirely sure what makes that different but I am using the way you wrote it as it is shorter by a few characters for the same result. My terminal now shows:

    msgId: 233638292636041218 <MiyakoProductions> test3

      Hi Miyako,

      I also noticed that it worked as $apiJson[0]->[0]->{'id'}; Not entirely sure what makes that different

      Compare these two:

      my @apiJson = decode_json( $apiContent ); my $msgID = $apiJson[0]->[0]{id}; # or # my $apiJson = decode_json( $apiContent ); my $msgID = $apiJson->[0]{id};

      In the first, you're taking the return value of decode_json, which I believe is just a single value, an arrayref or hashref, storing it in an array @apiJson which now just has a single element, and accessing that element with $apiJson[0]. In the second version, you're taking that single return value and storing it in the scalar $apiJson directly. (As far as I can tell decode_json behaves the same in list and array context.)

      Hope this helps,
      -- Hauke D