perl: Uncaught exception: malformed UTF-8 character in JSON string perl: Uncaught exception: malformed UTF-8 character in JSON string json json

perl: Uncaught exception: malformed UTF-8 character in JSON string


I expand on my answer in Know the difference between character strings and UTF-8 strings.


From reading the JSON docs, I think those functions don't want a character string, but that's what you're trying to give it. Instead, they want a "UTF-8 binary string". That seems odd to me, but I'm guessing that it's mostly to take input directly from an HTTP message instead of something that you type directly in your program. This works because I make a byte string that's the UTF-8 encoded version of your string:

use v5.14;use utf8;                                                 use warnings;                                             use feature     qw< unicode_strings >;use Data::Dumper;use Devel::Peek;use JSON;my $filename = 'hei.txt';my $char_string = qq( { "my_test" : "hei på deg" } );open my $fh, '>:encoding(UTF-8)', $filename;print $fh $char_string;close $fh;{say '=' x 70;my $byte_string = qq( { "my_test" : "hei p\303\245 deg" } );print "Byte string peek:------\n"; Dump( $byte_string );decode( $byte_string );}{say '=' x 70;my $raw_string = do {     open my $fh, '<:raw', $filename;    local $/; <$fh>;    };print "raw string peek:------\n"; Dump( $raw_string );decode( $raw_string );}{say '=' x 70;my $char_string = do {     open my $fh, '<:encoding(UTF-8)', $filename;    local $/; <$fh>;    };print "char string peek:------\n"; Dump( $char_string );decode( $char_string );}sub decode {    my $string = shift;    my $hash_ref2 = eval { decode_json( $string ) };    say "Error in sub form: $@" if $@;    print Dumper( $hash_ref2 );    my $hash_ref1 = eval { JSON->new->utf8->decode( $string ) };    say "Error in method form: $@" if $@;    print Dumper( $hash_ref1 );    }

The output shows that the character string doesn't work, but the byte string versions do:

======================================================================Byte string peek:------SV = PV(0x100801190) at 0x10089d690  REFCNT = 1  FLAGS = (PADMY,POK,pPOK)  PV = 0x100209890 " { \"my_test\" : \"hei p\303\245 deg\" } "\0  CUR = 31  LEN = 32$VAR1 = {          'my_test' => "hei p\x{e5} deg"        };$VAR1 = {          'my_test' => "hei p\x{e5} deg"        };======================================================================raw string peek:------SV = PV(0x100839240) at 0x10089d780  REFCNT = 1  FLAGS = (PADMY,POK,pPOK)  PV = 0x100212260 " { \"my_test\" : \"hei p\303\245 deg\" } "\0  CUR = 31  LEN = 32$VAR1 = {          'my_test' => "hei p\x{e5} deg"        };$VAR1 = {          'my_test' => "hei p\x{e5} deg"        };======================================================================char string peek:------SV = PV(0x10088f3b0) at 0x10089d840  REFCNT = 1  FLAGS = (PADMY,POK,pPOK,UTF8)  PV = 0x1002017b0 " { \"my_test\" : \"hei p\303\245 deg\" } "\0 [UTF8 " { "my_test" : "hei p\x{e5} deg" } "]  CUR = 31  LEN = 32Error in sub form: malformed UTF-8 character in JSON string, at character offset 21 (before "\x{5824}eg" } ") at utf-8.pl line 51.$VAR1 = undef;Error in method form: malformed UTF-8 character in JSON string, at character offset 21 (before "\x{5824}eg" } ") at utf-8.pl line 55.$VAR1 = undef;

So, if you take your character string, which you typed directly into your program, and convert it to a UTF-8 encoded byte string, it works:

use v5.14;use utf8;                                                 use warnings;                                             use feature     qw< unicode_strings >;use Data::Dumper;use Encode qw(encode_utf8);use JSON;my $char_string = qq( { "my_test" : "hei på deg" } );my $string = encode_utf8( $char_string );decode( $string );sub decode {    my $string = shift;    my $hash_ref2 = eval { decode_json( $string ) };    say "Error in sub form: $@" if $@;    print Dumper( $hash_ref2 );    my $hash_ref1 = eval { JSON->new->utf8->decode( $string ) };    say "Error in method form: $@" if $@;    print Dumper( $hash_ref1 );    }

I think JSON should be smart enough to deal with this so you don't have to think at this level, but that's the way it is (so far).


The docs say

$perl_hash_or_arrayref  = decode_json $utf8_encoded_json_text;

yet you do everything in your power to decode the input before passing it to decode_json.

use strict;use warnings;use utf8;use Data::Dumper qw( Dumper );use Encode       qw( encode );use JSON         qw( );for my $json_text (   qq{{ "my_test" : "hei på deg" }\n},   qq{{ "water" : "水" }\n},) {   my $json_utf8 = encode('UTF-8', $json_text);  # Counteract "use utf8;"   my $data = JSON->new->utf8->decode($json_utf8);   local $Data::Dumper::Useqq  = 1;   local $Data::Dumper::Terse  = 1;   local $Data::Dumper::Indent = 0;   print(Dumper($data), "\n");}

Output:

{"my_test" => "hei p\x{e5} deg"}{"water" => "\x{6c34}"}

PS — It would be easier to help you if you didn't have two pages of code to demonstrate a simple problem.


If there is a malformed UTF-8 character in your data, you can remove it in the following way (imagine that data are contained in data.txt):

iconv -f utf-8 -t utf-8 -c < data.txt > clean-data.txt

The -c option of iconv will silently remove all malformed characters.