Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6215045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:59:04+00:00 2026-05-24T06:59:04+00:00

UTF-8 characters are destroyed when processed with the JSON library (maybe this is similar

  • 0

UTF-8 characters are destroyed when processed with the JSON library (maybe this is similar to Problem with decoding unicode JSON in perl, however setting binmode only creates another problem).

I have reduced the problem down to the following example:

(hlovdal) localhost:/tmp/my_test>cat my_test.pl
#!/usr/bin/perl -w
use strict;
use warnings;
use JSON;
use File::Slurp;
use Getopt::Long;
use Encode;

my $set_binmode = 0;
GetOptions("set-binmode" => \$set_binmode);

if ($set_binmode) {
        binmode(STDIN,  ":encoding(UTF-8)");
        binmode(STDOUT, ":encoding(UTF-8)");
        binmode(STDERR, ":encoding(UTF-8)");
}

sub check {
        my $text = shift;
        return "is_utf8(): " . (Encode::is_utf8($text) ? "1" : "0") . ", is_utf8(1): " . (Encode::is_utf8($text, 1) ? "1" : "0"). ". ";
}

my $my_test = "hei på deg";
my $json_text = read_file('my_test.json');
my $hash_ref = JSON->new->utf8->decode($json_text);

print check($my_test), "\$my_test = $my_test\n";
print check($json_text), "\$json_text = $json_text";
print check($$hash_ref{'my_test'}), "\$\$hash_ref{'my_test'} = " . $$hash_ref{'my_test'} .  "\n";

(hlovdal) localhost:/tmp/my_test>

When running testing the text is for some reason crippeled into iso-8859-1. Setting binmode sort of solves it but then causes double encoding of other strings.

(hlovdal) localhost:/tmp/my_test>cat my_test.json 
{ "my_test" : "hei på deg" }
(hlovdal) localhost:/tmp/my_test>file my_test.json 
my_test.json: UTF-8 Unicode text
(hlovdal) localhost:/tmp/my_test>hexdump -c my_test.json 
0000000   {       "   m   y   _   t   e   s   t   "       :       "   h
0000010   e   i       p 303 245       d   e   g   "       }  \n        
000001e
(hlovdal) localhost:/tmp/my_test>
(hlovdal) localhost:/tmp/my_test>perl my_test.pl
is_utf8(): 0, is_utf8(1): 0. $my_test = hei på deg
is_utf8(): 0, is_utf8(1): 0. $json_text = { "my_test" : "hei på deg" }
is_utf8(): 1, is_utf8(1): 1. $$hash_ref{'my_test'} = hei p� deg
(hlovdal) localhost:/tmp/my_test>perl my_test.pl --set-binmode
is_utf8(): 0, is_utf8(1): 0. $my_test = hei på deg
is_utf8(): 0, is_utf8(1): 0. $json_text = { "my_test" : "hei på deg" }
is_utf8(): 1, is_utf8(1): 1. $$hash_ref{'my_test'} = hei på deg
(hlovdal) localhost:/tmp/my_test>

What is causing this and how to solve?


This is on a newly installed and up to date Fedora 15 system.

(hlovdal) localhost:/tmp/my_test>perl --version | grep version
This is perl 5, version 12, subversion 4 (v5.12.4) built for x86_64-linux-thread-multi
(hlovdal) localhost:/tmp/my_test>rpm -q perl-JSON
perl-JSON-2.51-1.fc15.noarch
(hlovdal) localhost:/tmp/my_test>locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
(hlovdal) localhost:/tmp/my_test>

Update: Adding use utf8 does not solve it, characters are still not processed right (although slightly different from before):

(hlovdal) localhost:/tmp/my_test>perl  my_test.pl
is_utf8(): 1, is_utf8(1): 1. $my_test = hei p� deg
is_utf8(): 0, is_utf8(1): 0. $json_text = { "my_test" : "hei på deg" }
is_utf8(): 1, is_utf8(1): 1. $$hash_ref{'my_test'} = hei p� deg
(hlovdal) localhost:/tmp/my_test>perl  my_test.pl --set-binmode
is_utf8(): 1, is_utf8(1): 1. $my_test = hei på deg
is_utf8(): 0, is_utf8(1): 0. $json_text = { "my_test" : "hei på deg" }
is_utf8(): 1, is_utf8(1): 1. $$hash_ref{'my_test'} = hei på deg
(hlovdal) localhost:/tmp/my_test>

As noted by perlunifaq

Can I use Unicode in my Perl sources?

Yes, you can! If your sources are
UTF-8 encoded, you can indicate that
with the use utf8 pragma.

use utf8;

This doesn’t do anything to your
input, or to your output. It only
influences the way your sources are
read. You can use Unicode in string
literals, in identifiers (but they
still have to be “word characters”
according to \w ), and even in custom
delimiters.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:59:05+00:00Added an answer on May 24, 2026 at 6:59 am

    The core of the problem was JSON’s expectation of an octet array instead of character string (solved in this question). However I was also missing several things related to unicode, like “use utf8”. Here is the diff required to make the code in the example work fully:

    --- my_test.pl.orig 2011-08-03 15:44:44.217868886 +0200
    +++ my_test.pl  2011-08-03 15:55:30.152379269 +0200
    @@ -1,19 +1,14 @@
    -#!/usr/bin/perl -w
    +#!/usr/bin/perl -CSAD
     use strict;
     use warnings;
     use JSON;
     use File::Slurp;
     use Getopt::Long;
     use Encode;
    -
    -my $set_binmode = 0;
    -GetOptions("set-binmode" => \$set_binmode);
    -
    -if ($set_binmode) {
    -        binmode(STDIN,  ":encoding(UTF-8)");
    -        binmode(STDOUT, ":encoding(UTF-8)");
    -        binmode(STDERR, ":encoding(UTF-8)");
    -}
    +use utf8;
    +use warnings qw< FATAL utf8 >;
    +use open qw( :encoding(UTF-8) :std );
    +use feature qw< unicode_strings >;
    
     sub check {
             my $text = shift;
    @@ -21,8 +16,9 @@
     }
    
     my $my_test = "hei på deg";
    -my $json_text = read_file('my_test.json');
    -my $hash_ref = JSON->new->utf8->decode($json_text);
    +my $json_text = read_file('my_test.json', binmode => ':encoding(UTF-8)');
    +my $json_bytes = encode('UTF-8', $json_text);
    +my $hash_ref = JSON->new->utf8->decode($json_bytes);
    
     print check($my_test), "\$my_test = $my_test\n";
     print check($json_text), "\$json_text = $json_text";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way in PostgreSQL to convert UTF-8 characters to similar ASCII characters?
Our XML feed gives us encoded UTF-8 characters inside ISO-8859-1 a file. This is
http://asexpress.de/ The problem is that all the special characters a destroyed. The Default Characterset
I found this question but it removes all valid utf-8 characters also (returns me
I am having a problem with utf-8 characters displaying correctly when being viewed with
i'm trying to print out a string of UTF-16 characters. i posted this question
Regex \w doesn't match utf-8 characters in Ruby 1.9.2. Anybody faced same problem? Example:
Is it possible to sort an array with Unicode / UTF-8 characters in PHP
Can base64 encoding applied to multibyte utf-8 characters ? How base64 encoded string is
Is it possible in C# to use UTF-32 characters not in Plane 0 as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.