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";
}
}