Search code examples
jsonperlhashref

How to parse complicated hash ref in perl?


I used JSON::Parse to decode a json file. When I dumped it to a file it looks as printed below. I need to extract the 'url' from each of the repositories. How can I go about doing that? I have tried to access the elements, but cannot seem to get to them. Any suggestions?

How I decoded the .json file:

my $json_hash_ref = {};
$json_hash_ref= json_file_to_perl ($REPO_JSON_FILE) or confess "No repos found";

This is the data dump:

$VAR1 = {
      'repositories' => [
                        {
                          'size' => 2970,
                          'watchers' => 49,
                          'created_at' => '2012-03-20T05:04:42Z',
                          'url' => 'http://github.cerner.com/aeon-core/aeon-topology',
                          'open_issues' => 0,
                          'followers' => 49,
                          'owner' => 'aeon-core',
                          'has_downloads' => 1,
                          'has_issues' => 0,
                          'language' => 'Java',
                          'pushed' => '2014-06-17T18:32:37Z',
                          'private' => ${\$VAR1->{'repositories'}[0]{'has_issues'}},
                          'name' => 'aeon-topology',
                          'score' => '1',
                          'has_wiki' => ${\$VAR1->{'repositories'}[0]{'has_issues'}},
                          'pushed_at' => '2014-06-17T18:32:37Z',
                          'description' => '',
                          'username' => 'aeon-core',
                          'created' => '2012-03-20T05:04:42Z',
                          'homepage' => '',
                          'forks' => 59,
                          'fork' => ${\$VAR1->{'repositories'}[0]{'has_issues'}},
                          'type' => 'repo'
                        }, 
                        {
                         .....
                        },...

Solution

  • my @urls = map $_->{url}, @{ $json_hash_ref->{repositories} };
    

    For good information on traversing deep data structures, read the perl references tutorial.