decode_json
expects JSON to be encoded using UTF-8.
As long as your source file is encoded using UTF-8, you have Perl decoded it using use utf8;
(as it should be). This means that your string contains Unicode characters, not the UTF-8 bytes that represent these characters.
As you have shown, you can encode a string before passing it to decode_json
.
use utf8; use Encode qw( encode_utf8 ); use JSON qw( decode_json ); my $data_json = qq( { "cat" : "Büster" } ); my $data = decode_json(encode_utf8($data_json));
But you can just tell JSON that the string is already decoded.
use utf8; use JSON qw( ); my $data_json = qq( { "cat" : "Büster" } ); my $data = JSON->new->utf8(0)->decode($data_json);
source share