So I got a string that has a backslash in it. \"kIurhgFBOzDW5il89\\/lB1ZQnmmY=\"
.
I tried adding an extra \'\\\', but JSON.stringify( \"kIurhgFBOzDW5i
JSON.parse(JSON.stringify("kIurhgFBOzDW5il89\\/lB1ZQnmmY="))
// "kIurhgFBOzDW5il89\/lB1ZQnmmY="
JSON.stringify
doesn't remove the backslash, it encodes it. When you use JSON.parse
on the other end, or whatever you do to decode your JSON, it will return the original string.
The backslash is escaping the forward slash. So JSON.stringify("\/")
returns "/"
since it sees an escaped forward slash, so its just a forward slash. JSON.stringify("\\/"
) sees a backslash being escaped, and then a forward slash next to that, so it returns "\/"
. You cannot preserve the "exact" string when you stringify, since parsing a json string will not escape characters, so you get back your original data, just unescaped.