Escape json in javascript

Clash Royale CLAN TAG#URR8PPPEscape json in javascript
Need to escape the following json
{
"xx": 'a',
"yy": "bb"
}
into the following structure in javascript
{rnt"xx": 'a',rnt"yy": "bb"rn}
I have tried the code suggestion from this link, How to escape a JSON string containing newline characters using JavaScript?
var request = {
"xx": "aaa",
"yy": "bb"
}
var myJSONString = JSON.stringify(request);
var myEscapedJSONString = myJSONString.replace(/\n/g, "\n").replace(/\'/g, "\'").replace(/\"/g, '\"').replace(/\&/g, "\&").replace(/\r/g, "\r").replace(/\t/g, "\t").replace(/\b/g, "\b").replace(/\f/g, "\f");
but not worked, please help.
Code has to escape like the following
What does "not worked" mean exactly? And much more important: What exactly are you trying to do? This really sounds like a bad idea or a workaround for something else.
– str
36 mins ago
Can you tell the reason you would want this ? May be there is a better way to achieve that. Serializing it like so, will cause issues while parsing.
– Dhananjai Pai
36 mins ago
I have tried this in my code, var myJSONString = JSON.stringify(request); var myEscapedJSONString = myJSONString.replace(/\n/g, "\n").replace(/\'/g, "\'").replace(/\"/g, '\"').replace(/\&/g, "\&").replace(/\r/g, "\r").replace(/\t/g, "\t").replace(/\b/g, "\b").replace(/\f/g, "\f"); request will contain the above given json structure { "xx": "aaa", "yy": "bb" }
– Rajesh Kumar
34 mins ago
@RajeshKumar I am seconding what Dhananjai said. There is probably a better solution to your problem, why do you want to do this?
– Marie
18 mins ago
2 Answers
2
Not sure why you want to do this, but stringify does exactly this, no regex or anything fancy,.. just stirngify your JSON string.
I've also sliced off the quotes..
var request = {
"xx": "aaa",
"yy": "bb"
}
var myJSONString = JSON.stringify(request, null, 2);
var myEscapedJSONString = JSON.stringify(myJSONString).slice(1, -1);
console.log(myEscapedJSONString);
var a= {
"xx": "aaa",
"yy": "bb"
}
var a1= {"foo":{"bar" : "bar2"}};
var nEscapped = JSON.stringify(a).replace("{", "{rn").replace(/.$/, "rn}");
console.log(JSON.stringify(nEscapped));
var nEscapped1 = JSON.stringify(a1).replace("{", "{rn").replace(/.$/, "rn}");
console.log(JSON.stringify(nEscapped1));
This is not the expected outcome. In addition, this code will break in so many situations, e.g.
{"foo":"{bar}"}.– str
26 mins ago
{"foo":"{bar}"}
@str Thank you for pointing. Corrected code.
– Hardik Shah
17 mins ago
The same limitations still apply.
– str
13 mins ago
@str, First of all, JSON is valid coming from the request as OP mentioned. The second thing is: replace will find the first matched element. So no problem with the first statement. and second, replace will always check for the last element as I have used regex.
– Hardik Shah
16 secs ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Where is your attempted code?
– Mamun
36 mins ago