Perl (Scripting) - JSON::Parse reason Not Found error Object Not Found

by
Jeremy Canfield |
Updated: January 19 2021
| Perl (Scripting) articles
Let's say you are using curl to obtain JSON.
#!/usr/bin/perl
use strict;
use warnings;
use JSON::Parse 'parse_json;
my $raw_json = 'curl http://www.example.com/api`;
Dumper can be used to show the results.
use Data::Dumper;
print Dumper $parsed_json;
Which may return something like this.
{"reason":"Not Found", "error":"Object Not Found"}
Usually, you will want to store the parsed json in a new variable.
my $parsed_json = parse_json($raw_json);
Dumper can be used to print the parsed json.
use Data::Dumper;
print Dumper $parsed_json;
Dumper should produce something like this.
$VAR1 = {
'reason' => 'Not Found',
'error' => 'Object Not Found'
};
To account for not found, the following if statement will exit if the "error" key is defined and the "error" key contains a value of "Object Not Found".
if ($json->{error} and $json->{error} eq "Object Not Found") {
print "not found \n";
exit 1;
}
Did you find this article helpful?
If so, consider buying me a coffee over at