6

I am doing a callout to a google script which takes in address and returns back geographic information (lat, lng etc) in the form of JSON string.

The problem is that in the returned JSON string " is replaced by \". So the JSON response is something like :

{\"results\":[{\"address_components\":[{\"long_name\":\"435\",\"types\":[\"street_number\"],\"short_name\":\"435\"},{\"long_name\":\"West Huron Street\",\"types\":[\"route\"],\"short_name\":\"W Huron St\"},{\"long_name\":\"Old West Side\",\"types\":[\"neighborhood\",\"political\"],\"short_name\":\"Old West Side\"},{\"long_name\":\"Ann Arbor\",\"types\":[\"locality\",\"political\"],\"short_name\":\"Ann Arbor\"},{\"long_name\":\"Washtenaw County\",\"types\":[\"administrative_area_level_2\",\"political\"],\"short_name\":\"Washtenaw County\"},{\"long_name\":\"Michigan\",\"types\":[\"administrative_area_level_1\",\"political\"],\"short_name\":\"MI\"},{\"long_name\":\"United States\",\"types\":[\"country\",\"political\"],\"short_name\":\"US\"},{\"long_name\":\"48103\",\"types\":[\"postal_code\"],\"short_name\":\"48103\"}],\"formatted_address\":\"435 West Huron Street, Ann Arbor, MI 48103, USA\",\"types\":[\"street_address\"],\"geometry\":{\"bounds\":{\"southwest\":{\"lng\":-83.75289409999999,\"lat\":42.28154259999999},\"northeast\":{\"lng\":-83.7528932,\"lat\":42.2815563}},\"viewport\":{\"southwest\":{\"lng\":-83.75424263029151,\"lat\":42.2802004697085},\"northeast\":{\"lng\":-83.75154466970851,\"lat\":42.2828984302915}},\"location_type\":\"RANGE_INTERPOLATED\",\"location\":{\"lng\":-83.75289409999999,\"lat\":42.28154259999999}}}],\"status\":\"OK\"}

This gives me an error- System.JSONException: Unexpected character ('\' (code 92)): was expecting double-quote to start field name at [line:1, column:3]

As you can see the " are replaced by \".

To counter this I am doing a replaceAll('\"', '"') but this is not helping. The final string still contains \"

I performed the same replace in my anonymous execute and it worked but it does not work in the apex class.

There already exists a similar question to this one on Stackoverflow but it did not help me much.

Any idea on why the replaceAll() is not working? Or any other way I can remove the \ from the response?

Thanks for the help.

2
  • Looks like the response has been escaped so it can be represented as a single string. How are you processing the response? Better to avoid the escaping if possible rather than to try to strip it out after.
    – Keith C
    Commented Apr 22, 2014 at 14:21
  • I am reading the response in a string. So its something like this: HttpResponse response = http.send(request); String jsonResponse = response.getBody(); Commented Apr 22, 2014 at 14:23

2 Answers 2

9

You can use the built in String.unescapeEcmaScript() method to remove the escape characters in that JSON string. This will also take care of any other escaped characters that may be in your string value.

// shortened your string value
string original = '{\"results\":[{\"address_components\":[{\"long_name\":\"435\",\"types\":[\"street_number\"]';

String s = original.unescapeEcmaScript();
system.debug(s);

DEBUG|{"results":[{"address_components":[{"long_name":"435","types":["street_number"]

3
  • Thanks Mark, it worked. I did not knew a method like this existed. Commented Apr 22, 2014 at 14:49
  • 3
    This method was part of the 'new' String methods added in Winter '13. There are similar methods for escaping and unescaping CSV, HTML and XML types. developer.salesforce.com/releases/release/Winter13/…
    – Mark Pond
    Commented Apr 22, 2014 at 14:54
  • Nice. Sometimes the new stuff takes a while to put in your tool belt. Thanks for pointing this out.
    – Eric
    Commented Apr 22, 2014 at 15:05
0

This will replace all the \ in the string

String s = '{\"results\":[{\"address_components\":[{\"long_name\":\"435\",\"types\":[\"street_number\"]';
s = s.replaceAll('[\\\\]','');
system.debug(logginglevel.error,s);

Do you have any control over the JSON output from the external system? You can have it format the output to not put in the \ character if you have any control over it

1
  • I do have control over the response from the google script, however the \ character is not put in from the google script. It happens when the response is read in the apex class in a string. Commented Apr 22, 2014 at 14:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .