Remove escape sequences from string

8,732 views
Skip to first unread message

Abhishek Aggarwal

unread,
Oct 12, 2018, 12:06:24 PM10/12/18
to Dart Misc

To decode API response string to JSON, json.decode() works fine. This will parse a JSON string similar to
{ “Response” : {“Responsecode” : “1” , “Response” : “Success”}}


But in my case, the response is in the form :
“{\”Response\” : {\”Responsecode\” : \”0\” , \”Response\” : \”Success\”}}”
json.decode() won’t work. I searched but couldn’t find how to unescape characters in a string. Please help

Valentin Olchowski

unread,
Oct 12, 2018, 12:15:27 PM10/12/18
to mi...@dartlang.org
There is a string methode "replaceAll(from, to)" where from is replaced by to. So you can try this on the json string like replaceAll("/", ""). 

--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/45641080-337b-4a3b-8c8b-20512363fd3c%40dartlang.org.

Abhishek Aggarwal

unread,
Oct 12, 2018, 12:33:35 PM10/12/18
to Dart Misc
You mean replace "\" with "".
It seems to work in this case, but if slash is itself in the json-data, will also be removed.
Also, if double quotes are present, then it will also remove its escape sequence.

For example,
{\"key\": \"abc\"de\"}, removing slashes will result in {"key": "abc"de"}, which will create problem while json parsing.

But the result should be {"key": "abc\"de"}, which clearly shows that the value of key is abc"de

Samuel Rawlins

unread,
Oct 12, 2018, 12:34:25 PM10/12/18
to General Dart Discussion
Yeah the String you have been given as a response is not properly encoded JSON. A JSON key must be a string. A string is defined as a `"` character, then characters, then a `"` character, _not_ the two-character sequence of `\` and `"`, followed by characters.

Replacing with String.replaceAll might sort of work... but it's error prone. If there is an escaped backslash inside a string, it would be deleted... yuck. Better maybe to fix the source of the problem, where the response is being encoded.

Abhishek Aggarwal

unread,
Oct 12, 2018, 12:43:13 PM10/12/18
to Dart Misc
I can't make change in the API. Also, I have been using these APIs for almost a year in my android projects, there's a java function StringEscapeUtils.unescapeJson(), which works perfectly for this response, but I couldn't find any such function in dart.

Abhishek Aggarwal

unread,
Oct 13, 2018, 1:53:50 AM10/13/18
to mi...@dartlang.org
Sorry, the example I gave earlier wasn't right

Suppose the value of key is abc"de. So, json string would be {"key": "abc\"de"}
And the response would /be like {\"key\": \"abc\\\"de\"}.
Hence, after unescaping the sequences, the desired json would be {"key": "abc\"de"}

Danny Tuppeny

unread,
Oct 13, 2018, 5:08:25 AM10/13/18
to Dart Misc
(I think my other email account got stuck in a mod queue, so a dupe might come through too...)

If you call json.decode() twice, I think you'll get what you need. The value you have is what you'd get if you encoded twice (eg. you encode something to a JSON string, then encoded that string). You may be able to fix it "upstream" by avoiding the second encoding, but decoding twice may be the simplest fix (esp. if there are already other consumers of the API).

Danny Tuppeny

unread,
Oct 13, 2018, 1:34:44 PM10/13/18
to Dart Misc
json.decode can decode strings too, so you should be able to just call it twice. The first time it'll return you a string (where the escape characters have been decoded) and the second time it'll decode that string into the map:

import 'dart:convert';

void main() {
  var a = r'''"{\"Response\" : {\"Responsecode\" : \"0\" , \"Response\" : \"Success\"}}"''';
  var b = json.decode(json.decode(a));
  print(b['Response']['Responsecode']); // 0
  print(b['Response']['Response']); // Success
}



On Friday, 12 October 2018 17:06:24 UTC+1, Abhishek Aggarwal wrote:

Abhishek Aggarwal

unread,
Oct 14, 2018, 11:32:37 AM10/14/18
to mi...@dartlang.org
I worked. Thanks a lot.

How can I convert this json array to list of a class (say Class Response having string variables as Responsecode and Response)?
Does dart has something similar to java's gson?

--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Abhishek Aggarwal

unread,
Oct 15, 2018, 2:33:37 AM10/15/18
to mi...@dartlang.org
Please post your answer in the following stackoverflow post, I will mark it as the right answer.

ravi teja

unread,
Oct 15, 2018, 3:54:31 AM10/15/18
to mi...@dartlang.org

Danny Tuppeny

unread,
Oct 15, 2018, 11:48:02 AM10/15/18
to mi...@dartlang.org
On Sun, Oct 14, 2018 at 4:32 PM Abhishek Aggarwal <abhi...@gmail.com> wrote:
How can I convert this json array to list of a class (say Class Response having string variables as Responsecode and Response)?
Does dart has something similar to java's gson?

The best option may depend on what type of project you're working on. There's some info on the Flutter website (including an answer to the exact question "Is there a GSON/Jackson/Moshi equivalent in Flutter?"):


I don't have much experience with any of this myself (the few projects I work on that do this have their own code-gen'd methods) but that's definitely where I'd start. Others here may have more experience and be able to offer some better advice (depending on whether your project is web/Flutter/VM).

Hope this helps!

Kisakye G. Charles

unread,
Aug 6, 2019, 5:44:48 PM8/6/19
to Dart Misc
am also getting a similar issue with a JSON API response below 

{
    "result": "successful",
    "data": {
        "id": 12,
        "name": "supported_countries",
        "value": "[{\"code\":\"BA\",\"name\":\"Bosnia & Herzegovina\",\"callingCodes\":[\"+387\"]},{\"code\":\"UG\",\"name\":\"Uganda\",\"callingCodes\":[\"+256\"]},{\"code\":\"CA\",\"name\":\"Canada\",\"callingCodes\":[\"+1\"]},{\"code\":\"AE\",\"name\":\"United Arab Emirates\",\"callingCodes\":[\"+971\"]},{\"code\":\"US\",\"name\":\"United States\",\"callingCodes\":[\"+1\"]},{\"code\":\"KE\",\"name\":\"Kenya\",\"callingCodes\":[\"+254\"]},{\"code\":\"GB\",\"name\":\"United Kingdom\",\"callingCodes\":[\"+44\"]}]",
        "secure": 0,
        "updated_at": "2018-10-13T14:20:05.000Z",
        "updated_by": null
    }
}

json.decode is failing to decode the mapping at "value",  could someone help me with this issue as I need to map this response using dart convert.



On Monday, October 15, 2018 at 6:48:02 PM UTC+3, Danny Tuppeny wrote:

Nate Bosch

unread,
Aug 6, 2019, 7:00:38 PM8/6/19
to General Dart Discussion
That example works for me.

https://dartpad.dartlang.org/4f62aa7188dfb5534933b37bb6b5422a

Note that if you are trying to put that string in a .dart file you'll need to use a raw string (see https://dart.dev/guides/language/language-tour#strings) for it to have the value you want, or make sure to have escaped slashes before the escaped quotes.

With r prefix, use \", without r prefix use \\\" for an escaped JSON quote character

    r'\"' == '\\\"'

--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
Reply all
Reply to author
Forward
0 new messages