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

Hello monks

I am trying to parse the response of a googleapis API. This is a json answer with an array of arrays structure. What am I doing wrong in this script? Why am I not able to iterate over @records? I am probably missing something very fundamental here...

use strict; use warnings; use Data::Dumper; use LWP::UserAgent; use JSON::PP; my $ua = LWP::UserAgent->new( ); $ua->agent("Mozilla"); my $segment = "Science | Research | Artificial Intelligence"; my $url='https://translate.googleapis.com/translate_a/single?client=gt +x&sl=en&tl=fr&dt=t&q=' . $segment; my $req = HTTP::Request->new(GET => $url); my $res = $ua->request($req); if ($res->is_success) { my $json_text= $res->content; my $decoded_json = decode_json( $json_text ); my @decoded_json=@$decoded_json; my @records = $decoded_json[0]; foreach my $record(@records){ print Dumper $record; print "Translation for @$record[1] is @$record[0]\n"; } } else { print "\nError connecting to server\n"; }

Replies are listed 'Best First'.
Re: Parsing Array of arrays from json file
by 1nickt (Canon) on Sep 12, 2021 at 10:07 UTC

    Hi,

    Your program is well structured and you correctly are printing out the array to debug. But the dump shows that each record is itself an array of arrays, so you need one more level of dereferencing.

    Change line 25 to

    print "Translation for $record->[0][1] is $record->[0][ +0]\n";

    Hope this helps!

    edit: got the indices the wrong way around


    The way forward always starts with a minimal test.

      Thank you. This is a good point. However, the script does not loop correctly through @records (only first element is printout)

        Since the structure is one level deeper than you thought, you need to add another loop. Try this:

        my @categories = $decoded_json[0]; foreach my $category (@categories){ foreach my $record (@$category) { print Dumper $record; print "Translation for $record->[0] is $record->[1 +]\n"; } }

        Hope this helps!


        The way forward always starts with a minimal test.
Re: Parsing Array of arrays from json file
by kcott (Archbishop) on Sep 13, 2021 at 04:49 UTC
    #!/usr/bin/env perl use strict; use warnings; my $VAR1 = ... your posted data here ...; my @records = @$VAR1; for my $index (0 .. $#{$records[0]}) { print "$records[0][$index][1] -> $records[0][$index][0]\n"; }

    Output:

    Science | -> Sciences | Research | -> Recherche | Artificial Intelligence -> Intelligence artificielle

    So, that gets the job done. With a better understanding of the data you start with, I expect that could probably be greatly simplified.

    Using the same name (i.e. decoded_json) for both a scalar and an array variable has a high chance of tripping you up somewhere in the code. You show Dumper $record in your posted code but later you say "Dump was of \@records".

    I'm also getting the sense that you're not overly familiar with handling arrays and arrayrefs. Take a look at perlintro; its "Perl variable types" section; the "Arrays" subsection; then follow the "Exhaustive information ..." links at the end (i.e. right before the "Variable scoping" heading).

    — Ken

Re: Parsing Array of arrays from json file
by LanX (Saint) on Sep 12, 2021 at 10:02 UTC

      The Dump of @records is:

      $VAR1 = [ [ [ 'Sciences | ', 'Science |', undef, undef, 3, undef, undef, [ [] ], [ [ [ 'f1ddc8227c19e618ac5e77c96270b0d5', 'en_fr_2021q1.md' ] ] ] ], [ 'Recherche | ', 'Research |', undef, undef, 3, undef, undef, [ [] ], [ [ [ 'f1ddc8227c19e618ac5e77c96270b0d5', 'en_fr_2021q1.md' ] ] ] ], [ 'Intelligence artificielle', 'Artificial Intelligence', undef, undef, 1 ] ] ];
        > The Dump of @records is:

        unlikely, maybe a dump of \@records ?

        In any case it looks like you got the nesting levels wrong, please try

         my @records = $decoded_json[0][0];

        or

         my @records = $decoded_json[0][0][0];

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery