4

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...)

  • How are you trying to display the data? By Visualforce? Standard page? Retrieving by API? Which API? – sfdcfox Apr 15 '15 at 2:29
  • @sfdcfox I'm making an HTTP callout to a third party API and retrieving JSON in response. When I assign that JSON to a string and print to log with system.debug(), it looks as expected, with double-quote characters around tokens. But when I assign same string to a text area field on a SF object, the " char is written as '& quot;' every time and I hit the size limit. – smohyee Apr 15 '15 at 5:20
4

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);

  • Yup, this is what I ended up using as well. Thanks for putting the answer down for others to see! – smohyee Apr 17 '15 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.