Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

SyntaxError: JSON.parse: unterminated string #143

Closed
stazna01 opened this issue Jan 23, 2013 · 20 comments
Closed

SyntaxError: JSON.parse: unterminated string #143

stazna01 opened this issue Jan 23, 2013 · 20 comments
Labels
Milestone

Comments

@stazna01
Copy link

I apologize in advance if I'm missing something obvious, but I have some pretty simple json...

{"links": [{"name": "AOL","url": "www.aol.com"}, {"name": "Yahoo!","url": "www.yahoo.com"}, {"name": "Microsoft","url": "www.microsoft.com"}, {"name": "CNN","url": "www.cnn.com"}, {"name": "Apple","url": "www.apple.com"}, {"name": "Digg","url": "www.digg.com"}, {"name": "Amazon","url": "www.amazon.com"}]}

...which I'm storing in a cookie.

I'm using "$.cookie.json = true;" which works fine, but when I try to read it back (Firefox 18 - Mac) I get the message...

SyntaxError: JSON.parse: unterminated string.

I used firebug to trace it and when it gets to the line...

value = value.slice(1, -1).replace('"', '"').replace('\', '');

The cookie variable result is...

"{"links\": [{\"name\": \"AOL\",\"url\": \"www.aol.com\"}, {\"name\": \"Microsoft\",\"url\": \"www.microsoft.com\"}, {\"name\": \"CNN\",\"url\": \"www.cnn.com\"}, {\"name\": \"Yahoo!\",\"url\": \"www.yahoo.com\"}, {\"name\": \"Apple\",\"url\": \"www.apple.com\"}, {\"name\": \"Digg\",\"url\": \"www.digg.com\"}, {\"name\": \"Amazon\",\"url\": \"www.amazon.com\"}]}"

If I replace all the "\" with "" it works fine. I've double checked that my json validates, so I'm thinking it must be an issue with the escaping replace statement (only the first one seems to be escaped properly). If there's anything else I can provide, please let me know. For now, I've found that using the "raw" option sidesteps this issue. Thanks so much for all of your work on this plugin. It's a huge help.

@carhartl
Copy link
Owner

It looks as if the value of the cookie starts with a ", otherwise it wouldn't end up in that if statement.

Could you look, in the debugger, at the value right before it gets written? Or am I overlooking something?

And then I'm wondering anyway whether json cookies would need the un-RFC-2068 treatment ever.

@carhartl
Copy link
Owner

This works fine for me (also in Firefox):
cookie_links

@carhartl
Copy link
Owner

Are you writing said cookie on the server?

@carhartl
Copy link
Owner

@stazna01

The only way I could trigger this is if I wrap the entire json in quotes, which were then no a json in the first place, such as:

$.cookie.json = true;
$.cookie('links', '{"links": [{"name": "AOL","url": "www.aol.com"}, {"name": "Yahoo!","url": "www.yahoo.com"}, {"name": "Microsoft","url": "www.microsoft.com"}, {"name": "CNN","url": "www.cnn.com"}, {"name": "Apple","url": "www.apple.com"}, {"name": "Digg","url": "www.digg.com"}, {"name": "Amazon","url": "www.amazon.com"}]}')

$.cookie('links') // throws SyntaxError: JSON.parse: unterminated string

You would need to remove those quotes if you're using it like this.

@carhartl
Copy link
Owner

I am closing this, I would need more information to proceed with this (and eventually reopen) but it seems to me it's incorrect usage.

@stazna01
Copy link
Author

Thank you so much for your replies. I updated to your newest build and the error message I'm getting now is different, but I have a feeling it's still related. I created a very simplified version of the page that has the json as a string in a variable and puts it int the cookie with no issues. When it tries to read it into the "test" variable it gets the following error...

JSON.parse: unexpected non-whitespace character after JSON data

It seems it chokes at...

cookie = JSON.parse(cookie);

Here's an example page - http://pomona.edu/carhartl/

@carhartl
Copy link
Owner

It's as I suspected. In that example you're using a string:

jsonObj = '{"links": [...]}' // That's a string

If you want to use the plugin's build-in JSON support, you would pass an object right away as value:

jsonObj = {"links": [...]} // That's an object that get's serialized automatically by the plugin

@stazna01
Copy link
Author

I fixed my example to use JSON.parse to feed it the object version of the string, which did seem to fix that part of the issue (THANK YOU)...but I'm noticing that if i test to see if a cookie exists or not (and it doesn't exist) I get that same error. I modified my example. It should have two alerts, one for the first cookie it finds and one for the second cookie it doesn't find. Instead, when it hits tries to check the second (non-existing) cookie I get the same error of...

JSON.parse: unexpected non-whitespace character after JSON data

...that again seems to choke at...

cookie = JSON.parse(cookie);

Here's the example page - http://pomona.edu/carhartl/

@FagnerMartinsBrack
Copy link
Collaborator

@carhartl
One more reason for #132

@carhartl
Copy link
Owner

I am still not convinced. If we were silently eating that exception imo it would have taken much longer to get to the root cause.

@FagnerMartinsBrack
Copy link
Collaborator

And now ppl can't use json = true with other cookies who are not in json format. Let's take the jsessionid which you should pass on every request if your cookies are disabled or store in the cookies otherwise.

If you still want to throw the exception we should detect a json format and ignore the json parse, don't you thinl it will add too much code to the codebase?

@carhartl
Copy link
Owner

I was thinking that one had to reset the json config when dealing with different type of cookies. That probably turns out as impractical. Then let's go the route eating the exception... :)

The thing is, we hardly need the json config flag at all in the first place.

@carhartl
Copy link
Owner

But how does catching the exception help with the case of trying to read a regular cookie while json = true. The returned value will be null, at least the way it's done in #145.

@stazna01
Copy link
Author

I just wanted to make sure I'm understanding things properly. When I first listed this issue I was able to check to see if a cookie existed or not by something like the following.

if ($.cookie('doesnt_exist') == 'null')

That worked until I updated my jquery.cookie.js code because I saw there had been some updates. Now that same code errors. The documentation says that checking for a cookie that doesn't exist should result in "null" but that no longer seems to happen because of code changes in the last few days. Sorry if I'm misunderstanding anything.

@carhartl
Copy link
Owner

Not quite right. You can and always could test wether a cookie exists or not - as can be verified by the tests like so:

if ( $.cookie('doesnt_exist') === null ) // NOTE null *not* 'null'

Or simply

if ( !$.cookie('doesnt_exist') )

Fwiw, in the new release there's now yet another way to do this, which might read better even:

var cookies = $.cookie();
if ( 'foo' in cookies ) // If true we know that the cookie named "foo" exists...

The problems you were facing were, as far as I could see, all related to wrong usage of json cookies, in that you tried to pass a string with json=true, and by that persisting invalid stringified JSON that will throw a syntax parse error upon trying to parse it back into an object - whereas the plugin expects a JSON object in such a case.

Last not least, what we're now trying to figure out is what's the best way to deal with these kind of errors, or invalid JSON in general and whether json=true was maybe not that usable as we thought it would be.

HTH

@carhartl
Copy link
Owner

My apologies, in fact you're right, this now will also throw an error:

$.cookie.json = true;
$.cookie('doesnt_exist') // => SyntaxError: JSON Parse error: Unexpected identifier "bar"

I will come up with a fix and a 1.3.1 release soon.

@FagnerMartinsBrack
Copy link
Collaborator

don't you think it will add too much code to the codebase?

You will need to check if the cookie is a json first.
Suppressing the exception would solve everything without too much check.

See those huge regex from https://github.com/douglascrockford/JSON-js/blob/master/json2.js. Thats what I am talking about.

@carhartl
Copy link
Owner

82c93eb

@carhartl
Copy link
Owner

@FagnerMartinsBrack Yeah I agree, that's beyond scope of the plugin.

@carhartl
Copy link
Owner

1.3.1 released. How in general we want to handle invalid JSON is discussed in #132...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants