Link to home
Start Free TrialLog in
Avatar of thomaszhwang
thomaszhwangFlag for United States of America

asked on

Use Unicode in Perl

I'm writing a program to export data from a mysql database to a JSON file using the code attached.  However the exported JSON file is not using unicode - foreign languages are parsed as question marks.  Any fix?  Thanks.
JSON.txt
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thomaszhwang

ASKER

Well, actually the data in my database is in Unicode.  If I use MySQL Workbench to retrieve the data, foreign languages are displayed correctly.

I believe the problem is in Perl, because the data is retrieve in Unicode, Perl output the data in Unicode as well.  However I also store the data in a variable in Perl and that variable is not in Unicode.
Any idea for this one?  Thanks.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is the code I use to generate the JSON file.

use DBI;
use utf8;
use PerlIO;
use DBIx::JSON;

my $dsn_io = "dbname=DB;host=localhost;port=3306";
my $json_io = DBIx::JSON->new($dsn_io, "mysql", 'user', 'password' )->do_select('SELECT * FROM TABLE;')->get_json;

open JSONFILE, '>:encoding(UTF-8)', '/var/www/html/data.json';
print JSONFILE "$json_io";
close(JSONFILE);

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, except for the foreign language part - it is replace with question marks.

Here is the JSON file: http://107.21.113.253/data.json
Thanks.