I am trying to parse a json file in perl. I want to extract the key "name" and corresponding value of it. my file looks like this
{
"data":[
{
"name":"ABC",
"id":"123",
},
{
"name":"PQR",
"id":"456",
},
{
"name":"XYZ",
"id":"789",
}
]
}
I am trying with below 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 not getting any output.
Can anyone tel me what is wrong ?