4

Need to escape the following json

{
    "xx": 'a',
    "yy": "bb"
}

into the following structure in javascript

{\r\n\t\"xx\": 'a',\r\n\t\"yy\": \"bb\"\r\n}

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

  • Backspace is replaced with \b
  • Form feed is replaced with \f
  • Newline is replaced with \n
  • Carriage return is replaced with \r
  • Tab is replaced with \t
  • Double quote is replaced with \"
  • Backslash is replaced with \
Rajesh Kumar
  • 247
  • 1
  • 6
  • 21
  • Where is your attempted code? – Mamun Jul 27 '18 at 11:58
  • 2
    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 Jul 27 '18 at 11:58
  • 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 Jul 27 '18 at 11:59
  • @RajeshKumar I am seconding what Dhananjai said. There is probably a better solution to your problem, why do you want to do this? – Marie Jul 27 '18 at 12:16
  • @Marie - Need to send escaped json to API service as request – Rajesh Kumar Jul 30 '18 at 12:25
  • @RajeshKumar Why do you think it needs to be escaped? And is it a service under your control? It doesn't really make sense to require it be escaped since they would have to remove the escapes to parse it. – Marie Jul 30 '18 at 14:44

2 Answers2

7

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);
Keith
  • 22,005
  • 2
  • 27
  • 44
  • Thanks for the suggestion, please check the updated question for better understanding of my requirement, I need to send the escaped json as request to API service – Rajesh Kumar Jul 30 '18 at 12:37
0

var a= {
  "xx": "aa",
  "yy": "bb"
}

var nA = JSON.stringify(a, null, "\r\t");
var nB = JSON.stringify(nA);
console.log(nB)

document.getElementById("showData").value = JSON.parse(nB);
<textarea id="showData" rows="10" cols="20"></textarea>
Hardik Shah
  • 4,042
  • 2
  • 20
  • 41
  • This is not the expected outcome. In addition, this code will break in so many situations, e.g. `{"foo":"{bar}"}`. – str Jul 27 '18 at 12:08
  • The same limitations still apply. – str Jul 27 '18 at 12:21
  • @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 Jul 27 '18 at 12:34
  • The OP's output contains whitespaces, yours does not. – str Jul 27 '18 at 15:06
  • @Hardik - Thanks for your code, please check my updated question, you can understand better of what I need. – Rajesh Kumar Jul 30 '18 at 12:29