[SalesForce] Storing JSON response in SF text field gives ‘"’ instead of double quotes (“)

For debug purposes, I'm trying to write the JSON body returned from an HTTP callout into an SF text area field.

My problem is, despite the JSON body being stored as a string variable, and despite it printing to system.debug just fine, when I save the same string to a text area field in SF, all the double quote characters are replaced with '& quot;' (I added the space between the & and quot; to avoid it being turned int ").

I tried doing a response.replace('"', '"') to manually change what appears to be an HTML special character, but it didn't work. I also tried using the EncodingUtil.urlDecode(x, 'JSON') method, but I don't actually know the name of the encoding type being used here (SF doesn't provide any list of options for the second parameter).

Can anyone clue me into what's going on here, and how to get the SF text field to interpret the JSON string the same as the debug logs / my apex code?

(And yes, I'm aware that debug logs are a thing, but I gave up on trying to configure them into something useful. I've spent more time 'debugging' debug logs than the code itself! 'Rapid prototyping platform' my gigantic co- but I digress…)

Best Answer

I had the same problem where a JSON string from another system was inserted in an text area field. Indeed, special characters where replaced with '&'-type of character strings. My solution was to use the unescapeHtml4(); method.

So:

JSONs = JSONs.unescapeHtml4(); Map<String, Object> m; m = (Map<String, Object>)JSON.deserializeUntyped(JSONs);