-1

If have the text:

"Conseil de discipline de l\u0027Ordre des comptables professionnels agréés du Québec"

How do I deal with the \u0027, which is obviously an apostrophe of some sort. How can I convert this to a readable character?

The data comes from JSON.

CJ7
  • 22,579
  • 65
  • 193
  • 321
  • http://search.cpan.org/~you/Encode-Escape-0.14/lib/Encode/Escape/Unicode.pm – dsm Aug 08 '16 at 04:03
  • @ysth Didn't work. I decoded the full JSON string using `decode_json` and it still printed out the literal "\u0027" when I dumped the object. – CJ7 Aug 08 '16 at 04:23
  • @CJ7 show your original json? it sounds like it may be doubly encoded – ysth Aug 08 '16 at 04:34
  • 2
    Possible duplicate of [How can I convert a Unicode codepoint (\uXXXX) into a character in Perl?](http://stackoverflow.com/questions/2667599/how-can-i-convert-a-unicode-codepoint-uxxxx-into-a-character-in-perl) – Denis Ibaev Aug 08 '16 at 07:07
  • How could this question possibly be 'too broad' ? – CJ7 Aug 08 '16 at 22:45

1 Answers1

1
use utf8;
use JSON::XS;
use open OUT => ':utf8';
my $decoder = JSON::XS->new->allow_nonref(1);
my $json_string = '"Conseil de discipline de l\\u0027Ordre des comptables professionnels agréés du Québec"';
printf "in: %s\nout: %s\n", $json_string, $decoder->decode($json_string);

works for me. (use utf8 needed for literal utf8 in the perl source, allow_nonref needed to decode a just a string, not an object or array)

If indeed you do have some doubly-encoded strings, you could do:

$string =~ s/\\u([[:xdigit:]]{4})/chr hex $1/g;
ysth
  • 96,171
  • 6
  • 121
  • 214