Reputation: 6173
I am trying to get key value pairs from following data. I am using this codee , however i am not getting any output . please suggest
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};
__DATA__
{
“org1” : {
“repo1” : [
“John”,
“Sam”,
“Sammy”,
],
“org2” : [
“repo2”,
“Samn”,
“Laggy”,
“Tester”,
]
}
}
Upvotes: 0
Views: 402
Reputation: 40748
Here is an example of how you can read the JSON data:
use feature qw(say);
use strict;
use warnings;
use Data::Dumper;
use JSON;
my $str = do { local $/; <DATA> };
my $json = JSON->new;
my $data = $json->decode($str);
print Dumper($data);
__DATA__
{
"org1" : {
"repo1" : [
"John",
"Sam",
"Sammy"
]
},
"org2" : {
"repo2" :[
"Samn",
"Laggy",
"Tester"
]
}
}
Output:
$VAR1 = {
'org2' => {
'repo2' => [
'Samn',
'Laggy',
'Tester'
]
},
'org1' => {
'repo1' => [
'John',
'Sam',
'Sammy'
]
}
};
Upvotes: 2