Stephens Stephens - 1 month ago 6
Perl Question

Printing the URL from Pingdom with perl

I'm looping through the decoded Json returned from Pingdom's Get Detailed Check Information API. I'm trying to print the url from the Json and I've having a hard time.

Here is the response Json:

{
"check" : {
"id" : 85975,
"name" : "My check 7",
"resolution" : 1,
"sendtoemail" : false,
"sendtosms" : false,
"sendtotwitter" : false,
"sendtoiphone" : false,
"sendnotificationwhendown" : 0,
"notifyagainevery" : 0,
"notifywhenbackup" : false,
"created" : 1240394682,
"type" : {
"http" : {
"url" : "/",
"port" : 80,
"requestheaders" : {
"User-Agent" : "Pingdom.com_bot_version_1.4_(http://www.pingdom.com/)"
}
}
},
"hostname" : "s7.mydomain.com",
"status" : "up",
"lasterrortime" : 1293143467,
"lasttesttime" : 1294064823
}
}


Here is the code that I'm using to print the url:

my $decoded_info = decode_json($json) or die "Failed to decode!\n";
foreach my $check( $decoded_info->{check}) {
print "$decoded_info->{$check}->{type}->{http}->{url}\n";
}


Please let me know if you need anymore information in order to help. I have also read the perlreftut but, I still need to understand it better.

Thank you,

Mike

Answer

You want

$decoded_info->{check}->{type}->{http}->{url}    # ok

and $check has the value of

$decoded_info->{check};

so you should be using

$check->{type}->{http}->{url}                    # ok

instead of

$decoded_info->{$check}->{type}->{http}->{url}   # BAD

By the way,

my $check = $decoded_info->{check};
...

is simpler than

foreach my $check( $decoded_info->{check}) {
    ...
}
Comments