in reply to Array of hashes?

I thought it was an array of hashes, but apparently, it is not. How do I access it?

It is in fact an array of hashes, but I suspect the confusion might be coming from the fact that it's a reference to an array of hashes, as indicated by the square brackets [...]. In other words, $VAR1 here is a reference to an array, and the elements of that array are the hashes. You can access it like any reference to an array, i.e. $VAR1->[0]{Lang9} is 'well'. See perldsc, perlreftut, and perlref.

Note that you haven't shown your code, but in case it looks like print Dumper(@array);, then I would guess that you're probably doing something like my @array = decode_json(...);, which isn't quite correct, as decode_json functions typically don't return an array, but instead a reference to an array or a hash. So in other words, you should be doing my $arrayref = decode_json('[...]');, and then use the dereferencing operators that are described in the links above - for example, for my $elem (@$arrayref) or my $first = $arrayref->[0];.

Edit: Added a little bit more info to first sentence.

Replies are listed 'Best First'.
Re^2: Array of hashes?
by Anonymous Monk on Jan 04, 2021 at 17:42 UTC

    Thank you. I read the json data like this (verbose here):

    my $data = decode_json( $json ); my $definition= $data->{'myData'}; my @definition=@$definition; print Dumper \@definition; #trying to access the data structure with: foreach my $record (@definition) { print $record->{Lang4}; }

    The json data is produced like this (according to documentation). I could access/change the script originating the json file, if needed:

    while (my $row = $sth->fetchrow_hashref) { push @definition, $row; } print objToJson( { myData => \@definition} );
      I read the json data like this

      That code works for me (Update: assuming the code you showed in the parent node is what produced the output shown in the root node). Please show a representative Short, Self-Contained, Correct Example that reproduces the problem you're having and that includes sample input, expected output, and actual output including any error messages (How do I post a question effectively?).

      use warnings; use strict; use Data::Dumper; use JSON::PP qw/decode_json/; my $json = <<'JSON'; { "myData" : [ { "Lang4":"ok", "Lang9":"well", "Lang7":null, "Lang6":null }, { "Lang4":"one", "Lang9":"two", "Lang7":null, "Lang6":null } ] } JSON my $data = decode_json( $json ); my $definition= $data->{'myData'}; my @definition=@$definition; print Dumper \@definition; foreach my $record (@definition) { print $record->{Lang4}, "\n"; }

      Output:

      $VAR1 = [ { 'Lang6' => undef, 'Lang9' => 'well', 'Lang4' => 'ok', 'Lang7' => undef }, { 'Lang6' => undef, 'Lang9' => 'two', 'Lang7' => undef, 'Lang4' => 'one' } ]; ok one

        Happy that at the end my approach was okay. Did not worked though in the final application with some more passing of the REF among subrutines. Cleaning the code helped. Thank you.