JSON: Unterminated String Literal Error

Posted March 2, 2007 at 7:21 AM

Tags: ColdFusion, Javascript / DHTML, AJAX

I was just fooling around with some JSON (JavaScript Object Notation) and kept this error whenever I tried to return a string value that contained line breaks:

"unterminated string literal"

I know what the error was (especially as FireFox's FireBug kicks sooo much butt with AJAX), but I couldn't understand how to get around it. Is it possible that JSON values cannot have line breaks? Then it hit me! Javascript line breaks are represented by the "\n" character, NOT an actual line break. Therefore, values such as:

"Libby was this really cute
trainer that I used to work
with."

... should actually be:

"Libby was this really cute\ntrainer that I used to work\nwith."

I was able to update my ColdFusion to JSON algorithm, specifically my StringToJSON() UDF to escape all the special characters:

 Launch code in new window » Download code as text file »

  • <!--- Return a string object. --->
  • <cfreturn (
  • """" &
  • ToString( ARGUMENTS.Data ).ReplaceAll(
  • "(['""\\\/\n\r\t]{1})",
  • "\\$1"
  • ) &
  • """"
  • ) />

Now, it works just fine. This AJAX and JSON stuff is very cool and much more fun once you get a better idea of how it is all working.

Download Code Snippet ZIP File

Comments (12)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Adobe ColdFusion 8.0.1 Update - Helping Programmers To Be Signifanctly Less Girlie - Download ColdFusion 8 Update 8.0.1 Now.

Reader Comments

Hey Ben, I don't know if you've ever seen it or care for it, but there's a ColdFusion implementation of JSON called CFJSON that I'm now maintaining. You can see it at http://www.epiphantastic.com/cfjson/ . I'm actually planning to release an update today, I may actually have to include the fix you found for your own library!

Posted by Thomas Messier on Mar 2, 2007 at 8:27 AM


Yup, this a common workaround for almost any web app that contains text with line breaks. Thanks for posting your little code snippet.

Posted by Boyan on Mar 2, 2007 at 9:02 AM


@Thomas,

I like what you have done. I don't quite translate everything the same way. For instance, my CF Query object becomes a simple array of objects; it does not keep the column-first style that CF does. I also don't pass record count and stuff.

But, I do like what you have done. I have some ideas that might help improve it (or maybe not - I am mostly new to AJAX stuff). I would like to clean up what I have and send it to you for review. I will shoot you an email or something later this weekend or next week.

Posted by Ben Nadel on Mar 2, 2007 at 9:23 AM


@Ben,

I saw the same need as you to have a query as an array instead of a structure, and there's actually an argument that can be passed to make it return it as an array. Not well documented, I admit...

I was about to release a new version, but after reading your post I did a few tests with special characters and ran into one problem when there were double quotes in a string. I just tracked down the problem and found a fix, but I'm a bit puzzled as to why CF is behaving as it is. I'll try to send you an email when I have a sec later to describe the problem, maybe you'll have a clue as to why things are so.

Posted by Thomas Messier on Mar 2, 2007 at 10:08 AM


Why aren't you using the standard 'jsStringFormat()' function?

Posted by Edward A Savage Jr on Mar 2, 2007 at 4:30 PM


@Ed,

JSStringFormat() just escapes certain characters. JSON is much more complex. It converts entire ColdFusion objects to Javascript, not just strings.

Posted by Ben Nadel on Mar 2, 2007 at 5:59 PM


Hey Ben, if you downloaded CFJSON earlier today, I recommend that you download the latest version that I just put up. There were some serious bugs that were fixed. And if you have any suggestions I'd love to hear them.

Posted by Thomas Messier on Mar 2, 2007 at 7:52 PM


"JSStringFormat() just escapes certain characters."

That's exactly what I was referring to. You were getting errors because of carriage returns/line feeds. jsStringFormat() converts those to '\n'. If you use that function on string values, you don't have to roll your own regular expression. Thomas Messier's CFJSON component uses the jsStringFormat() function.

Posted by Edward A Savage Jr on Mar 2, 2007 at 9:01 PM


This information at last that has returned me on the necessary way :)

Posted by Nata on Mar 3, 2007 at 6:33 AM


@Ed,

I apologize. You were completely right about JSStringFormat(). I was not familiar with this function and I see now what you are saying. I am going to implement this. Thanks for the tip.

Posted by Ben Nadel on Mar 3, 2007 at 9:39 AM


Very helpful, even to those of us not using ColdFusion. Thanks! That 'invalid label' solution also saved me a lot of headache earlier. Props.

Posted by Josh Allmann on Apr 16, 2007 at 1:19 PM


Heck yeah! It's all about people helping people :)

Posted by Ben Nadel on Apr 16, 2007 at 1:20 PM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting