Encoding NewLines & Quotes in jSON

by Randall 7/20/2010 11:04:00 AM

Today I attempted to send a giant amount of user-entered text from a standard HTML page, via jQuery's .ajax() method - passing a jSON packet to a REST-enabled WCF service.

It was failing.

Turns out the newlines were destroying the jSON formatting, so I needed to find a way to preserve the information for the server.  I ran across a blog post dealing with this here,  and he suggested the following bit of code:

function escapeNewLineChars(valueToEscape) {
    if (valueToEscape != null && valueToEscape != "") {
        return valueToEscape.replace(/\n/g, "\\n");
    } else {
        return valueToEscape;

It didn't work.

Turns out, after fiddling with it, the "replace with" value needed to be wrapped in single (not double) quotes. So, in order to prepare your text for transmittal via jSON, try this:

function fixMyUserEnteredData(giantBlobOfText) {

giantBlobOfText = $.trim(giantBlobOfText.replace(/\n/g, '\\n')); // converts the newlines and trims the string

return giantBlobOfText.replace(/\"/g, '\\"')); // converts all double quotes


That did the trick.


Currently rated 1.4 by 1456 people

  • Currently 1.440249/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,


jQuery & Visual Studio 2010 Beta

by Randall 5/29/2009 2:30:00 PM

Today I decided to check out the jQuery support within Visual Studio 2010.  I had been having difficulty in getting IntelliSense to work correctly in Visual Studio 2008 SP1 as described by Scott Guthrie here.  Turns out that VS wasn't allowing jQuery and jQuery UI to play along nicely.  If I removed the script include for jQuery UI, IntelliSense would begin functioning for jQuery as it was supposed to. However, I'm currently trying to learn both of them so I want it to work properly.

After firing up Visual Studio 2010 Beta, I created a new Web Application Project: 

By the way, you might be happy to see that the option for a website has been removed and you're now forced to choose a web application project.  Personally I'm glad to see this.

Getting back to it, I immediately went into my code behind and started typing up a script block - I didn't see anything any jQuery IntelliSense at all.  DOH.  jQuery is SUPPORTED, not PROVIDED.  After including the scripts for jQuery and jQuery UI, IntelliSense began working beautifully providing support for both.

For jQuery ...


... and for jQuery UI ... 


Currently rated 3.3 by 6 people

  • Currently 3.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Powered by BlogEngine.NET
Theme by Mads Kristensen

About the author

Name of author Randall Sexton
Currently a .Net developer for Bechtel Corporation in Oak Ridge, TN.

E-mail me Send mail


<<  July 2015  >>

View posts in large calendar


    Recent comments

    Don't show



    Logo Credit

    My logo was taken from CodingHorror.
    Jeff Atwood © Copyright 2007


    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2015

    Sign in