I am trying to parse a JSON file in Perl. I want to extract the key "name" and corresponding value of it.
My JSON file looks like this:
{
"data":[
{
"name":"ABC",
"id":"123",
},
{
"name":"PQR",
"id":"456",
},
{
"name":"XYZ",
"id":"789",
}
]
}
I am trying with this code:
#/usr/lib/perl
use lib qw( ..);
use LWP::Simple;
use JSON;
my $filename = '/file.txt';
my $data;
if (open (my $json_str, $filename))
{
local $/ = undef;
my $json = JSON->new;
$data = $json->decode(<$json_str>);
close($json_stream);
}
print $data->{name};
But, I'm not getting any output.
Can anyone tell me what is wrong?