Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda Java return string with slashes

I have problem with returning string from Lambda after

JSONObject.toString

in return i have

"{\"Key2\":\"Value2\",\"Key1\":\"Value1\"}" 

instead of

"{"Key2":"Value2","Key1":"Value1"}"

Can somebody explain how to exclude these slashes?

like image 883
Yevhen Avatar asked Nov 07 '22 13:11

Yevhen


1 Answers

If you really need to remove them ...

yourstring.replace("\\", "");

However, those "stupid slashes" are necessary if you are treating your response as a string, as they escape your " character. Specifically, without those, your compiler would behave as such:

"{"     // is a string
Key2    // Not known by Java
":"     // is a string
Value2  // Not known by Java
","     // is a string
Key1    // Not known by Java
":"     // is a string
Value1  // Not known by Java
"}"     // is a string

By escaping your " character with a backslash, you are mentionning to your compiler that it should not be taken as an end of string nor a begin. Thus, asking it to only take in account the first and last ".

like image 90
Yassine Badache Avatar answered Nov 14 '22 22:11

Yassine Badache