Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether hash key contain JSON in perl?

Tags:

json

perl

I am sending a request to the endpoint url ,from there i am getting the response in case of success in form of JSON,but if it fails it return certain text .

Sending request:

$data->{response} = $self->{_http}->send($myData);

So before doing this:

$resp = from_json($data->{response});

i want to check whether the reponse is in json format or not .How we can handle this in Perl kindly help in this

like image 582
Pooja Dubey Avatar asked Jan 29 '15 07:01

Pooja Dubey


1 Answers

You can catch exception thrown by from_json(),

my $resp;
my $ok = eval { $resp = from_json("{}"); 1 };
$ok or die "Not valid json";

or simpler,

my $resp = eval { from_json("rrr") };
$resp // die "Not valid json";
like image 59
mpapec Avatar answered Nov 15 '22 03:11

mpapec