I am very new to perl so please help me out in following
I have one perl script to execute telnet command. This script receives response from server as string. Actually server create a json string and then it sends to client program but client program is showing it as string
Question : How I can convert this string to json and read data from this json string.
I have json string with structure similar to following
[{"success":"21","data":[{"name":"tester","lastname":"project"}]}]
Following are the last lines where I have tried to convert it to json
@lines = $telnet->waitfor('/$/');
my @json;
@json = @{decode_json(@lines)};
It prints output as below
HASH(0x1af068c)
Thanks in advance !!!
Here is a snippet to convert the JSON. Modified to catch errors.
use strict;
use warnings;
use JSON::XS;
use Try::Tiny;
use Data::Dumper::Concise;
my $data = qq<[{"success":"21","data":[{"name":"tester","lastname":"project"}]}]>;
my $decoded;
try {
$decoded = JSON::XS::decode_json($data);
}
catch {
warn "Caught JSON::XS decode error: $_";
};
print Dumper $decoded;
I think there is a simpler one:
use JSON ();
$content = "{WHATEVER JSON CONTENT}";
$content = JSON->new->utf8->decode($content);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With