JSON: Unterminated String Literal Error
Posted March 2, 2007 at 7:21 AM by Ben Nadel
"unterminated string literal"
"Libby was this really cute
trainer that I used to work
... 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:
- <!--- Return a string object. --->
- <cfreturn (
- """" &
- ToString( ARGUMENTS.Data ).ReplaceAll(
- ) &
- ) />
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.
What Other People Are Searching For
[ local search ]
[ local search ] json and coldfusion
[ local search ] json string values
[ local search ] line break json value
[ local search ] json string literal with line breaks
[ local search ] json string with returns
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!
Yup, this a common workaround for almost any web app that contains text with line breaks. Thanks for posting your little code snippet.
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.
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.
Why aren't you using the standard 'jsStringFormat()' function?
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.
"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.
This information at last that has returned me on the necessary way :)
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.
Very helpful, even to those of us not using ColdFusion. Thanks! That 'invalid label' solution also saved me a lot of headache earlier. Props.
Heck yeah! It's all about people helping people :)
You are not giving us anything to work with.
I dont know anything about none of this stuff, but every time I go to tagged or something, it say the same error that everybody is talking about here... Does anybody mind explaining this stuff to me because its beginning to become a true pain the the rectum... lol...
What do you mean "go to tagged"? What are you doing when you get this error?
Sweet, it works. makes total sense.
Man, I just wanted you to know that this little tidbit helped me after two hours of banging my head against the monitor, wondering why my ExtJS-provided widget wasn't correctly parsing the JSON that it received from the server...
...and, yes, Firebug *does* rock!
Firebug does rock! I can hardly imagine having to go develop without it again.
Thank you for all your informative blogs.
I have the same issue trying to parse data from a query to json. I have tried everything and no luck.
Is there a place where I can download StringToJSON() udf?
Are you talking about a server-side UDF? What language are you using?
Yes, I was talking about the server-side UDF.
I'm using ColdFusion. I figured it out, basically escaping all "funny" characters.
PS I enjoy your blogs...
You could just use CF8's serializeJSON().
True, but this work for CF7 :(
I keep getting the message unterminated string constant when I try to pull up a web site. Would downloading this help? And if so where do I save it to after unzipping?
No - this only works for a programmer; as a user, you have no control over the error.
Ben, once again, you are the master. I have a script using the jQuery autocomplete plugin and using CF to get query results to return and then populate related form elements. The data being returned in one of the fields had single quotes, line feeds, carriage returns, you name it. I had spent 4 hours with jsStringFormat and trying to write my own jsonStringFormat function. I kept having issues with getting consistent results. Your comment about the serializeJSON in CF8 saved my day. I quickly changed my output with that function and all data is returned clean and well formatted when populated in the textarea.
Thanks again for all your posts. They are appreciated.
thanks. i was struggling to see the issue with google maps json strings, but this pointed me in the right direction.