[SOLVED] Specials characters conversion on HTML string from JSON or http

Hello,

I am facing issue with strings from http response.
I grab somes quotes and proverbs from an online service, via their API, http binding and JSONPATH transformation.

This is my item definiton :

String ChuckNorrisFact "Chuck Norris Fact = %s"  { http="<[http://www.chucknorrisfacts.fr/api/get?data=tri%%3Aalea;type%%3Atxt;nb%%3A1:10000000:JS(chucknorisfact.js)]" }

The final result is a string, often containing somes specials characters like à, é, etc (i am french). This string is in HTML format, so my final widget in Habpanel always display special character like this : &agrave, &egrave, etc.

I am trying to convert this result to correctly display proverbs. But i dont find solution.
I have tried with string conversion (%s, %c) and with javascript transformation (unescape(), decodeURI()) but nothing seems to work.

Can you explain me how to convert this string in the right form to correctly display it in habpannel ?

Thanks a lot !

You will have to use something that does a manual translation I presume, so something like:
(Bare in mind I typed this on a phone and made it up without testing, and this is after numerous edits…)

(function(i) {
    var chars = [/&agrave;/g, /&egrave;/g];
    var symbols = ['à', 'è'];
    for(let c = 0; c<chars.length; c++){
        i = i.replace(chars[c], symbols[c]);
    }
    return i;
})(input)

Nobody is perfect!!

BTW, I am French too…

:sweat_smile:

I thinked there is a decoder or transform included directly in openhab but it seems there isn’t.
I will try RolfV solution. Thanks

Ok so after somes try, here is my JS function ton convert correctly most of special html entities characters :


(function(i) {	
    var json = JSON.parse(i);
    var jsonkeyvalue = json.jsonkeyname;

    var list = [
        ['\"', /&quot;/g],
        ['\'', /&apos;/g],
        ['\'', /&#039;/g],
        ['&', /&amp;/g],
        ['<', /&lt;/g],
        ['>', /&gt;/g],
        ['¡', /&iexcl;/g],
        ['¢', /&cent;/g],
        ['£', /&pound;/g],
        ['¤', /&curren;/g],
        ['¥', /&yen;/g],
        ['¦', /&brvbar;/g],
        ['§', /&sect;/g],
        ['¨', /&uml;/g],
        ['©', /&copy;/g],
        ['ª', /&ordf;/g],
        ['«', /&laquo;/g],
        ['¬', /&not;/g],
        ['­', /&shy;/g],
        ['®', /&reg;/g],
        ['¯', /&macr;/g],
        ['°', /&deg;/g],
        ['±', /&plusmn;/g],
        ['²', /&sup2;/g],
        ['³', /&sup3;/g],
        ['´', /&acute;/g],
        ['µ', /&micro;/g],
        ['¶', /&para;/g],
        ['·', /&middot;/g],
        ['¸', /&cedil;/g],
        ['¹', /&sup1;/g],
        ['º', /&ordm;/g],
        ['»', /&raquo;/g],
        ['¼', /&frac14;/g],
        ['½', /&frac12;/g],
        ['¾', /&frac34;/g],
        ['¿', /&iquest;/g],
        ['×', /&times;/g],
        ['÷', /&divide;/g],
        ['À', /&Agrave;/g],
        ['Á', /&Aacute;/g],
        ['Â', /&Acirc;/g],
        ['Ã', /&Atilde;/g],
        ['Ä', /&Auml;/g],
        ['Å', /&Aring;/g],
        ['Æ', /&AElig;/g],
        ['Ç', /&Ccedil;/g],
        ['È', /&Egrave;/g],
        ['É', /&Eacute;/g],
        ['Ê', /&Ecirc;/g],
        ['Ë', /&Euml;/g],
        ['Ì', /&Igrave;/g],
        ['Í', /&Iacute;/g],
        ['Î', /&Icirc;/g],
        ['Ï', /&Iuml;/g],
        ['Ð', /&ETH;/g],
        ['Ñ', /&Ntilde;/g],
        ['Ò', /&Ograve;/g],
        ['Ó', /&Oacute;/g],
        ['Ô', /&Ocirc;/g],
        ['Õ', /&Otilde;/g],
        ['Ö', /&Ouml;/g],
        ['Ø', /&Oslash;/g],
        ['Ù', /&Ugrave;/g],
        ['Ú', /&Uacute;/g],
        ['Û', /&Ucirc;/g],
        ['Ü', /&Uuml;/g],
        ['Ý', /&Yacute;/g],
        ['Þ', /&THORN;/g],
        ['ß', /&szlig;/g],
        ['à', /&agrave;/g],
        ['á', /&aacute;/g],
        ['â', /&acirc;/g],
        ['ã', /&atilde;/g],
        ['ä', /&auml;/g],
        ['å', /&aring;/g],
        ['æ', /&aelig;/g],
        ['ç', /&ccedil;/g],
        ['è', /&egrave;/g],
        ['é', /&eacute;/g],
        ['ê', /&ecirc;/g],
        ['ë', /&euml;/g],
        ['ì', /&igrave;/g],
        ['í', /&iacute;/g],
        ['î', /&icirc;/g],
        ['ï', /&iuml;/g],
        ['ð', /&eth;/g],
        ['ñ', /&ntilde;/g],
        ['ò', /&ograve;/g],
        ['ó', /&oacute;/g],
        ['ô', /&ocirc;/g],
        ['õ', /&otilde;/g],
        ['ö', /&ouml;/g],
        ['ø', /&oslash;/g],
        ['ù', /&ugrave;/g],
        ['ú', /&uacute;/g],
        ['û', /&ucirc;/g],
        ['ü', /&uuml;/g],
        ['ý', /&yacute;/g],
        ['þ', /&thorn;/g],
        ['ÿ', /&yuml;/g],];
    for(var c = 0; c<list.length; c++){
        jsonkeyvalue  = jsonkeyvalue.replace(list[c][1], list[c][0]);
    }
    return jsonkeyvalue;
})(input)

It should convert most of html entities characters. Thanks to RolfV.
Hope this will help somebody :wink: