DecodeURIComponent is not supporting for %uXXXX encoded component

1.1k views Asked by At

DecodeURIComponent is not supporting for few encoded component

I am sending JD with bulletin format in my json , in restapi . So I am encoding the jd and sending. this works properly without any issues.But when I am trying to decode the encoded JD , I am getting error as URI malformed

var jd = "Where are bullets most often used?

 - Technical writing
 - Reference works
 - Notes
 - Presentations";


var json ={
"job":encodeURIComponent(escape(jd));

}

Decoding :

var jd = decodeURIComponent(jd);

thi is my encoded jd I am getting from response.

Where%20are%20bullets%20most%20often%20used%3F%0A%uF0B7Technical%20writing%0A%uF0B7Sub%20bullet%0A%uF0B7Reference%20works%0A%uF0B7Notes%0A%uF0B7Presentations%0A%uF0B7Lists%0AAn%20alternative%20method%20is%20to%20use%20a%u807Dnumbered%20list%3A%0A1.Technical%20writing%0A2.Reference%20works%0A3.Notes%0A4.Presentations%0A5.Lists
2

There are 2 answers

0
John Velasquez On BEST ANSWER

you have to unescape it first

var jd = decodeURIComponent(unescape(json.job));
3
georgeawg On

Avoid the use of escape().

From the Docs:

escape()

The escape() function computes a new string in which certain characters have been replaced by a hexadecimal escape sequence.

Description

The escape function is a property of the global object. Special characters are encoded with the exception of: @*_+-./

The hexadecimal form for characters, whose code unit value is 0xFF or less, is a two-digit escape sequence: %xx. For characters with a greater code unit, the four-digit format %uxxxx is used.

Deprecated. Not for use in new websites.

— MDN JavaScript Reference - escape()

From Wikipedia:

There exists a non-standard encoding for Unicode characters: %uxxxx, where xxxx is a UTF-16 code unit represented as four hexadecimal digits. This behavior is not specified by any RFC and has been rejected by the W3C. The third edition of ECMA-262 still includes an escape function that uses this syntax, along with encodeURI and encodeURIComponent functions, which apply UTF-8 encoding to a string, then percent-escape the resulting bytes.

— Wikipedia - Percent encoding - Non-standard implementations