1

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
    "}];
William Walseth
  • 2,803
  • 1
  • 23
  • 25
  • Write an array in php and use json_encode and save the pain – Aysennoussi Jul 17 '14 at 23:42
  • and IF you want to test your json syntax there are lot of tools to validate it http://jsonlint.com/ – Aysennoussi Jul 17 '14 at 23:43
  • It valid (only sometimes fails). I'm adding in user content that I want to be sure is "clean" from a JSON perspective. – William Walseth Jul 17 '14 at 23:45
  • BTW, I'm using .NET, and JSON.net – William Walseth Jul 17 '14 at 23:46
  • 1
    OK, Always think of building arrays first, then convert them to json Please take a look here http://stackoverflow.com/questions/9016595/return-json-using-c-sharp-like-php-json-encode – Aysennoussi Jul 17 '14 at 23:49
  • To color what Sekai said, you should build your C# *object* (anonymous or strongly-typed) completely first. Then convert it in a single serialization call. – Erik Philips Jul 17 '14 at 23:51
  • @WilliamWalseth you say you're using JSON.net, but you never include that in your code samples. The 3 major JSON serializers for .NET all handle the cases you have. – Matthew Jul 17 '14 at 23:52
  • @Matthew. Thanks. My original data is in XML, then I convert via XSL to JSON. Since my XML is attribute (not element) based, I couldn't (or could figure out how) to use JSON.net serializer – William Walseth Jul 17 '14 at 23:58
  • Hmm, can't convert my dataset into an object, just to get rid of some junk. I guess I'll look in the source for NEWTON to see how they build clean JSON from XML. – William Walseth Jul 18 '14 at 00:08
  • Seems like duplicate http://stackoverflow.com/questions/12042302/serializing-strings-containing-apostrophes-with-json-net – Joy George Kunjikkuru Jul 18 '14 at 00:29

1 Answers1

2

Use JSON.NET.

var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(new { requestcomment = "Please complete \"Education Provided\" for all Medications" });

and...

var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(new { requestcomment = "Please complete\nEducation Provided\nHistory\nAllergies" });
Brendan
  • 3,415
  • 24
  • 26
  • This is an excellent library from what I have used it for so far. – JoelC Jul 18 '14 at 13:01
  • @Brendan, thanks. I ended up writing my own clean-up function, which is getting smarter and smarter. You're answer is the best for my original question. Unfortunately this doesn't work for me since my data set contains both stings and JSON that I want returned to the browser as JSON. Any thoughts on that? – William Walseth Jul 18 '14 at 17:53
  • Can you post a Gist of what you're trying to do? I'd avoid trying to mix hand-coded JSON with generated code and just try and create it all with JSON.NET. Maybe you could parse the existing JSON using JSON.NET then create the output from there? – Brendan Jul 20 '14 at 00:37
  • So, I've got a simple data structure (survey questions and answers) which is stored in JSON in my database. I also have some meta data (who took the survey, date time, etc.). I'm returning a number of surveys back to the browser so people can review answers, and resubmit. I currently run my SQL query and get an XML dataset. Then I take the XML and convert to JSON with XSL. My "json.xsl", inspects the names of the fields, and knows to make some as strings and keep others as JSON. – William Walseth Jul 22 '14 at 00:38