Unescape String APEX

apeximperativeapexjsonlightning-web-componentsstring

Premise: I have a list of string which I have to pass it from LWC to APEX and in APEX I have to put this List in SOQL where clause to get the data. Finally I have to return the SOQL result list back to LWC.
I'm using imperative way of calling the APEX method and sending the parameters to APEX. At LWC side I have used methodName({strLst: JSON.stringify(strLstLWC)});

In each of the record of "strLstLWC", I have some special characters. For example, one value could be 'A || B' while another value could be 'someText #### someOtherText'.

Problem: At APEX side I'm getting this list of string but all of my special characters are getting convereted into their respective ASCII values. For example, A || B is getting converted into A || B and so on. And to use these values in my SOQL I dont need the ASCII values.

I can use some regEx to convert special character to normal characters or else can use replaceAll to get the desired result, but is there any standard way of achieving this?

Note: I tried unescape methods of String but there was no luck.

System.debug('unescapeHtml3 ++++++ ' + a.unescapeHtml3());
System.debug('unescapeHtml4 ++++++ ' + a.unescapeHtml4());
System.debug('unescapeXml ++++++ ' + a.unescapeXml());
//System.debug('unescapeJava ++++++ ' + a.unescapeJava());
//System.debug('unescapeUnicode ++++++ ' + a.unescapeUnicode());

Best Answer

Don't JSON-encode your strings; the platform does this for you automatically.

methodName({strLst: strLstLWC});

By double-encoding, you're making more work for yourself when you later have to double-decode it on the server side (one of these is done automatically for you).

Note that if you're passing in a list of strings, just say so in your apex:

@AuraEnabled public static void methodName(String[] strLst) {

The platform is smart enough to handle data conversions for you automatically. Do not try to outsmart the system, unless you happen to be running into some very specific bugs that you're trying to work around.

In general, the data types in LWC components are compatible with the server-side versions of those data types, so manual intervention, including serializing to JSON is rarely, if ever, needed.

Related Topic