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

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter 6d2fb5 in the box below so that we can be sure you are a human.