Replace single backslash with double. How does it effect string that already have double backslah

nishtha3
Kilo Contributor

Hi ,

I'm using the below code to escape single backslash that is present in my JSON payload

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

This works fine when the payload contains single backslash. eg {"name": "\xyz"}

This gets converted to {"name": "\\xyz"}

but the cases where there are 2 backslash already {"name": "\\abc"} the output is {"name": "\\\\abc"}

As far a I know as backslash is special character in JS, we need to escape each single (\) with double(\\), and that is what my code does, why does it effect the already double(\\).

Nisha

6 REPLIES 6

Hi asifnoor,

It did not work. Still getting 4 backslash if 2 are present in JSON. Could you tell me how the code given by you works?

can you share your json once.