-2

I'm really struggling to understand how to navigate a JSON object. I'm using JSON.pm, and I am beating my head on my desk attempting to access each "name" parameter and store the first index of it's "values" array.

I'm having trouble understanding the basic structure, how array/hash references work, and how to iterate over them to access the values that I need. If someone could help point me in the right direction... I'm pretty lost/confused at this point.

This is the sample data I'm working with (a Facebook open graph object).

{
  "data": [
    {
      "id": "219127134886593/insights/page_impressions/week",
      "name": "page_impressions",
      "period": "week",
      "values": [
        {
          "value": 31600,
          "end_time": "2013-03-07T08:00:00+0000"
        },
        {
          "value": 31979,
          "end_time": "2013-03-08T08:00:00+0000"
        },
        {
          "value": 29517,
          "end_time": "2013-03-09T08:00:00+0000"
        }
      ],
      "title": "Weekly Total Impressions",
      "description": "Weekly The number of impressions seen of any content associated with your Page. (Total Count)"
    },
    {
      "id": "219127134886593/insights/page_impressions_organic/week",
      "name": "page_impressions_organic",
      "period": "week",
      "values": [
        {
          "value": 23587,
          "end_time": "2013-03-07T08:00:00+0000"
        },
        {
          "value": 23858,
          "end_time": "2013-03-08T08:00:00+0000"
        },
        {
          "value": 22813,
          "end_time": "2013-03-09T08:00:00+0000"
        }
      ],
      "title": "Weekly Organic impressions",
      "description": "Weekly The number of times your posts were seen in News Feed or ticker or on visits to your Page. These impressions can be by people who have liked your Page and people who haven't. (Total Count)"
    },
  ],
  "paging": {
    "previous": "https://graph.facebook.com/219127134886593/insights/page_impressions,page_impressions_organic,page_impressions_viral,page_storytellers/week?since=1362383320&until=1362642520",
    "next": "https://graph.facebook.com/219127134886593/insights/page_impressions,page_impressions_organic,page_impressions_viral,page_storytellers/week?since=1362901720&until=1363160920"
  }
}

And this is what I am working on to access the different elements:

foreach my $data ( $decoded_json->{data} ) {
            foreach my $item ( @$data ) {
                $list{ $item->{'name'} } = $item->{'values'};
                print "Value 3: @($item->{'values'})[3]\n";
            }
        }
Ryan Fisher
  • 1,485
  • 1
  • 19
  • 32
  • _“I'm having trouble understanding the basic structure, how array/hash references work, and how to iterate over them to access the values that I need.”_ – then you should maybe consult a beginners tutorial that explains the basic PERL data types and how to use them. – CBroe Mar 12 '13 at 09:16
  • I understand, and I am. However it's not clicking when it comes to working with JSON objects. I'm hoping for an example to help me understand. – Ryan Fisher Mar 12 '13 at 09:26
  • There is no such thing as "JSON objects". JSON is a text format. For references in Perl, see [`perldoc perlreftut`](http://perldoc.perl.org/perlreftut.html). – melpomene Mar 12 '13 at 10:03
  • Thank you for the help, makes far more sense now. – Ryan Fisher Mar 12 '13 at 10:12
  • I stumbled on this stackoverflow question. The answer by Greg Bacon is a great breakdown of what a hash reference in Perl is. http://stackoverflow.com/questions/1817394/whats-the-difference-between-a-hash-and-hash-reference-in-perl – Ryan Fisher Mar 14 '13 at 05:17

2 Answers2

1
for my $item (@{ $decoded_json->{data} }) {
    $list{ $item->{'name'} } = $item->{'values'};
    print "Value 4: $item->{'values'}[3]\n";
}

See http://p3rl.org/REF for more about references.

melpomene
  • 84,125
  • 8
  • 85
  • 148
0

Success!

foreach my $data ( $decoded_json->{data} ) {
    foreach my $item ( @$data ) {
        $list{ $item->{'name'} } = ${ $item->{'values'} }[1]->{'value'};
        print "Name: $item->{'name'}\n";
        print "Value 3: ${ $item->{'values'} }[1]->{'value'}\n";
    }
}

Thank you to: dispersiondesign.com thegeekstuff.com

Thanks people, condensed:

foreach my $item ( @{ $decoded_json->{data} } ) {
    $list{ $item->{'name'} } = $item->{'values'}->[1]->{'value'};
    print "Name: $item->{'name'}\n";
    print "Value 3: $item->{'values'}->[1]->{'value'}\n";
}
Ryan Fisher
  • 1,485
  • 1
  • 19
  • 32