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 \
6
  • Where is your attempted code? Commented Jul 27, 2018 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. Commented Jul 27, 2018 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" } Commented Jul 27, 2018 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? Commented Jul 27, 2018 at 12:16
  • @Marie - Need to send escaped json to API service as request Commented Jul 30, 2018 at 12:25

2 Answers 2

8

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);

Sign up to request clarification or add additional context in comments.

1 Comment

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
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>

5 Comments

This is not the expected outcome. In addition, this code will break in so many situations, e.g. {"foo":"{bar}"}.
The same limitations still apply.
@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.
The OP's output contains whitespaces, yours does not.
@Hardik - Thanks for your code, please check my updated question, you can understand better of what I need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.