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

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

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |