in reply to Re: Array of hashes?
in thread Array of hashes?

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} );

Replies are listed 'Best First'.
Re^3: Array of hashes?
by haukex (Archbishop) on Jan 04, 2021 at 17:49 UTC
    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.