I have this JSON string
$json_string = qq{{"error" : "User $user doesn't exist."}};
which I construct in a low level way, so to speak.
How do I encode it using the JSON module?
Right now I encode hashes like this
use JSON;
my $json_string;
my $json = JSON->new;
$json = $json->utf8;
my $data;
$data->{users} = $json->encode(\%user_result);
$data->{owners} = $json->encode(\%owner_result);
$json_string = to_json($data);
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json_string;
Ratna is right – you can not encode a simple string (unless you put it in a list or hash)
Here are a couple of variants to encode your string: