Reputation: 93
I am trying to decode a simple JSON string in perl but it doesn't conform to JSON that I am used to.
The JSON is for Browshot and looks like:
{
"52967":{
"priority":"1",
"status":"finished",
"screenshot_url":"https://www.browshot.com/screenshot/image/52967?scale=1&key=MY_KEY",
"id":"52967",
"url": "http://www.google.com/",
"size": "screen",
"width": "1024",
"height": "768",
"request_time": "1312106111",
"started": "1312258803994",
"load": "1312258829461",
"content": "1312258829661",
"finished": "1312258829681",
"instance_id": "10",
"response_code": "200",
"final_url": "http://www.google.com/",
"content_type":"text/html",
"scale": "1",
"cost": "0",
"referer": "",
"post_data": "",
"cookie": "",
"delay": "5",
"flash_delay": "10",
"script": "",
"shared_url": ""
},
"52969":{
"priority":"1",
"status":"finished",
"screenshot_url":"https://www.browshot.com/screenshot/image/52969?scale=1&key=MY_KEY",
"id":"52969",
"url": "http://www.google.org/",
"size": "screen",
"width": 1024,
"height": "768",
"request_time": "1312106111",
"started": "1312258803994",
"load": "1312258829461",
"content": "1312258829661",
"finished": "1312258829681",
"instance_id": "10",
"response_code": "200",
"final_url": "http://www.google.org/",
"content_type":"text/html",
"scale": "1",
"cost": "0",
"referer": "",
"post_data": "",
"cookie": "",
"delay": "5",
"flash_delay": "10",
"script": "",
"shared_url": ""
},
"52971":{
"priority":"1",
"status":"processing",
"screenshot_url":"https://www.browshot.com/screenshot/image/52971?scale=1&key=MY_KEY",
"id":"52971",
"url": "http://www.google.de/",
"size": "screen",
"width": "1024",
"height": "768",
"request_time": "1312106111",
"started": "1312258803994",
"load": "1312258829461",
"content": "1312258829661",
"finished": "1312258829681",
"instance_id": "10",
"response_code": "200",
"final_url": "http://www.google.de/",
"content_type":"text/html",
"scale": "1",
"cost": "0",
"referer": "",
"post_data": "",
"cookie": "",
"delay": "5",
"flash_delay": "10",
"script": "",
"shared_url": ""
},
"52970":{
"priority":"1",
"status":"finished",
"screenshot_url":"https://www.browshot.com/screenshot/image/52970?scale=1&key=MY_KEY",
"id":"52970",
"url": "http://www.google.fr/",
"size": "screen",
"width": "1024",
"height": "768",
"request_time": "1312106111",
"started": "1312258803994",
"load": "1312258829461",
"content": "1312258829661",
"finished": "1312258829681",
"instance_id": "10",
"response_code": "200",
"final_url": "http://www.google.fr/",
"content_type":"text/html",
"scale": "1",
"cost": "0",
"referer": "",
"post_data": "",
"cookie": "",
"delay": "5",
"flash_delay": "10",
"script": "",
"shared_url": ""
},
"52968":{
"priority":"1",
"status":"finished",
"screenshot_url":"https://www.browshot.com/screenshot/image/52968?scale=1&key=MY_KEY",
"id":"52968",
"url": "http://www.google.com/",
"size": "screen",
"width": "1024",
"height": "768",
"request_time": "1312106111",
"started": "1312258803994",
"load": "1312258829461",
"content": "1312258829661",
"finished": "1312258829681",
"instance_id": "10",
"response_code": "200",
"final_url": "http://www.google.com/",
"content_type":"text/html",
"scale": "1",
"cost": "0",
"referer": "",
"post_data": "",
"cookie": "",
"delay": "5",
"flash_delay": "10",
"script": "",
"shared_url": ""
}
}
Normally I would use:
my $records = decode_json($JSON);
my @screens = @{ $records };
foreach my $f ( @screens ) {
print "$f->{'screenshot_url'}|\n";
}
But this obviously does not work.
I'm sure it's simple but I must be missing the mark here and cannot find a solution, all help appreciated.
Upvotes: 0
Views: 1271
Reputation: 385847
To iterate over a hash, one normally iterates over the keys returned by keys
.
for my $id (keys(%$records)) {
my $record = $records->{$id};
say $record->{screenshot_url};
}
Upvotes: 1