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

how can i parse array in json::xs i can parse hash but failed to access arrays

$data = $responde->{data}; $data_info = $responde->{data_info}->{info};
print $data; #prints status print $data_info; #prints user

how can i access names, age, location, country, email

{ "data" : "status", "data_info" : { "info" : "user", "del_info" : [ { "names" : "john doe", "age" : 28, "location" : [ { "country" : "ca", } ], "email" : [ "john@email.com" ], "cf" : 1.0, "pr" : null } ] } }

Replies are listed 'Best First'.
Re: json::xs arrays
by choroba (Cardinal) on Mar 29, 2017 at 13:25 UTC
    my $email = $response->{data_info}{del_info}[0]{email}[0];

    or, if there are more than one,

    my @emails = @{ $response->{data_info}{del_info}[0]{email} };

    and if there are more than one del_info's:

    my @emails = map @{ $_->{email} }, @{ $response->{data_info}{del_info} + };

    ($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,

      it only parse email not accessing other data in array

      its the problem i was facing

      { "data" : "status", "data_info" : { "info" : "user", "deluser" : [ { "names" : "john doe", "age" : 28, "location" : [ { "country" : "ca", } ], "email" : [ "john@email.com" ], "cf" : 1.0, "pr" : null } ] } }

        Note that the JSON string you present in this post is different from the one presented in the OP and so does not match with choroba's code here. Is
            "del_info" : [ ... ]
        or
            "deluser" : [ ... ]
        correct? (Everything else seems the same.)

        Update: Another link that might be helpful in dealing with the complex data structures that can be produced by JSON parsing is the Perl Data Structures Cookbook (or perldsc).


        Give a man a fish:  <%-{-{-{-<

Re: json::xs arrays
by NetWallah (Canon) on Mar 29, 2017 at 14:09 UTC
    It may be easier to conceptualize, if you pull out a group of data, then extract from that:
    my $del_info_0 = $responde->{data_info}{del_info}[0]; my ($names,$age,$location) = @{$del_info_0}{qw|names age location|}; # + Hash-ref slice my $country = $location->[0]{country};
    Here is an intro to references.

            ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall

      ARRAY<0X1ec64bc> thats the response

        Show us your code .. What did you try to print ?
        Which part got that response ?

                ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall