How to convert unicode text to readable text

89 views Asked by At

I use UrlFetch to get web content in Google AppsScript. Problem is that the content I get is: Kurulu\u015f Osman - \u0627\u0644\u0645\u0624\u0633\u0633 \u0639\u062b\u0645\u0627\u0646

When I use Logger.log(content) I get: Kuruluş Osman - المؤسس عثمان This is what I want to get when I do a POST UrlFetchApp.

When I POST the content with UrlFetchApp.fetch(content) I still get: Kurulu\u015f Osman - \u0627\u0644\u0645\u0624\u0633\u0633 \u0639\u062b\u0645\u0627\u0646

How to get the Logger.log(content) also being POST:ed with UrlFetchApp command?

Code:

var result = UrlFetchApp.fetch(urlA).getContentText(); // Gives, in Debug mode: Kurulu\u015f Osman - \u0627\u0644\u0645\u0624\u0633\u0633 \u0639\u062b\u0645\u0627\u0646
Logger.log(result); // Gives: Kuruluş Osman - المؤسس عثمان

var payload = JSON.stringify({content: result});
var params = {
    method: "POST",
    payload: payload,
    muteHttpExceptions: true,
    contentType: "application/json"
};

UrlFetchApp.fetch(urlB, params);

I get this at the receiving end: Kurulu\u015f Osman - \u0627\u0644\u0645\u0624\u0633\u0633 \u0639\u062b\u0645\u0627\u0646

2

There are 2 answers

1
dav1app On

You have multiple ways of doing so.

The thing is that Unicode is decoded to avoid compatibility errors with different platforms you may want to check your logs in.

It is widespread to have consoles that only support ASCII, for example.

My suggestion is to streamline your logs. I know that this can be annoying, but as soon as you get used to a log platform (Datadog, Sentry, PaperTrail...) this goes always.

1
Private Flashman On
Updated Solution coded by myself. Please comment.
function fixText(content) {

 var regex1 = /(\\)u([0-9A-Fa-f]){4}/gm;
 var findChar = [...content.matchAll(regex1)];
 var nbroffindChar = findChar.length;
 var outPutLenght = content.length;
 var count = 0;

if(nbroffindChar >0){
  
 for(b=0;b<nbroffindChar;b++){
      
  var text = content.substring(count,outPutLenght);
  var check = text.search(regex1);    
  var code = text.substring(check + 2,check + 6);
  var hexToDec = parseInt(code, 16);
  var char = String.fromCharCode(hexToDec);
  var textOutput = content.replace(code, char); 
      
  count = check + 1; 
  content = textOutput;

 }

  var regex2 = /(\\)u/gm;
  textOutput = textOutput.replace(regex2, "");
  content = textOutput;

 }

  return content;

}