I'm running a project that is returning dynamically build JSON. Recently I discovered that carriage returns, and double quotes the JSON string invalid (can't be loaded via an AJAX). I'm now replacing the parameter in question, removing any double quotes, and such, but I feel like I'm playing whack-a-mole.
Is there a better way?
In XML, for example, if I'm building a node, I can just call setAttribute( strMyJunkyString ), and it safely creates an attribute that will never break the XML, even if it has special characters, entities, etc.
Is there some sort of MakeStringJSONSafe() function, to remove anything that would break the array ([{}"\r\n])...
Here's a couple examples of a broken strings that my program is creating...
// String built with " included.
var t1 = [{"requestcomment":"Please complete "Education Provided" for all Medications "}];
// String built with returns embedded included.
var t2 = [{"requestcomment":"Please complete
Education Provided
History
Allergies
"}];