in reply to Re: how to acces data in JSON
in thread how to acces data in JSON

Thank you so much!! That's so useful!! But now I have a new problem. I'm completely begginer and I can't understand it. The problem is that I have to read the json file from my script. I've try like this:
use strict; use warnings; binmode STDOUT, ":utf8"; use utf8; use JSON; my $json; { local $/; open my $fh, "<", "struc_cover_edu.json"; $json = <$fh>; close $fh; } my $j = JSON->new->decode($json); for my $s (@$j) { next unless $s->{PROT_NAME} eq 'XPA'; for my $p (@{$s->{COVERTURA}}) { print $p->{PSTART},"\n"; } }
But an ERROR appears: Not an ARRAY reference at script.pl line 20. (line 20 is that one: for my $s (@$j) ) how could I solve it? thanks thanks thanks!!!

Replies are listed 'Best First'.
Re^3: how to acces data in JSON
by hdb (Monsignor) on Apr 28, 2013 at 14:36 UTC

    Can you please put your code into code tags to make it more readible. Generally, such problems can be most easily analyzed with the module Data::Dumper. So add something like

    use Data::Dumper; ... print Dumper($j);

    to your code to see what is contained in $j. To be an array reference it has to be surrounded by square brackets [ ].