1

If there is a cookie on our website called cabbages and $.cookie("cabbages") returns this:

"purchaseType":"NONE","futurePurchaseType":"NONE","id":73041988,"unlimitedStatus":null,"hasFuturePrivilege":false,"corpUser":false,"suspendedStatus":null,

What is the prescribed or conventional way to get back the value of id? I want the ID of the visitor so in this example I'd like to write some Javascript that returns 73041988.

Doug Fir
  • 19,971
  • 47
  • 169
  • 299
  • I don't think it'll be meaningful to call any method as "the normal" or "common way" to do this, because that will be just weasel talk, unless it can be supported by data from some research survey done among all javascript developers around the world. It would be much better if you post a few of the different methods in your mind and ask which one looks better. – rineez Dec 07 '14 at 16:39
  • Contents of your `cabbages` cookie looks very close to json object syntax to me. So `json.parse()` would be naturally the way I would take, but what other options do you have in mind? – rineez Dec 07 '14 at 16:49
  • 1
    @rineez I've changed my question. Looking over it again I didn't like what I was asking either. I imagined that given how popular cookies are there must be some methods to use with them to make grabbing this kind of data more convenient than writing out a function, as much of the internet seems to suggest. Having said that, what method would you recommend? – Doug Fir Dec 07 '14 at 16:52
  • @rineez JSON.parse did the trick - thank you. I've never used JSON before but have heard of it. Is what I just did applicable to most cookies? I'll look into it. Put another way, what would have prevented json.parse from working? – Doug Fir Dec 07 '14 at 16:54
  • OK, So may be I should post it as answer. – rineez Dec 07 '14 at 18:36
  • Well it saved me a lot of time and saved me from fiddling with functions. Certainly a good answer from my point of view – Doug Fir Dec 07 '14 at 18:51

1 Answers1

2

Contents of your cabbages cookie looks very close to json object syntax to me. So JSON.parse() would be naturally the way I would take. You just need to add curly braces to that string to make it valid json object syntax.

Actually this has got nothing to do with cookies. If any variable contains data having syntax similar to this, you can always go for JSON.parse() to extract it in to a javascript variable.

Json objects look like:

{name1:value1,name2:value2,name3:value3}

Similarly a json array looks like:

[value1,value2,value3]

and you could use JSON.parse for any data having json sytax.

You can see some more good examples of JSON syntax in links below.

http://json.org/example

http://www.tutorialspoint.com/json/json_syntax.htm

Please note that this JSON API which provides native support for json serialization in javascript may not be available in some older browsers and the function call will fail.

As you mentioned $.cookie() in your question, I guess that your project is already using JQuery. So you better use jQuery.parseJSON(), which makes use of JSON.parse where the browser provides a native implementation, and also provides a fall back parser when browser support is not available.

This Stack Overflow thread has more details about Native JSON support in browsers.

Community
  • 1
  • 1
rineez
  • 753
  • 13
  • 33
  • Thank you for the answer, I will update to use JQuery.parseJSON given the browser compatibility issues you mentioned. – Doug Fir Dec 07 '14 at 19:36