JSON parsing cookie contents

41 views
Skip to first unread message

Gustav van der Merwe

unread,
Aug 18, 2015, 3:45:47 PM8/18/15
to lif...@googlegroups.com
I've been trying to grab data out of a cookie and parse that, the value is:

{stamp:'XmooaL03kY1V1nk+nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA=='%2Cnecessary:true%2Cpreferences:true%2Cstatistics:false%2Cmarketing:false}

which after url decoding is:

{stamp:'XmooaL03kY1V1nk nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA==',necessary:true,preferences:true,statistics:false,marketing:false}

which gives me the following error:

net.liftweb.json.JsonParser$ParseException: unknown token s
Near: {st
at net.liftweb.json.JsonParser$Parser.fail(JsonParser.scala:235) ~[lift-json_2.11-3.0-M6.jar:3.0-M6]
at net.liftweb.json.JsonParser$Parser.nextToken(JsonParser.scala:322) ~[lift-json_2.11-3.0-M6.jar:3.0-M6]

Is that JSON not valid? Seems to be to my mind...

Regards,
Gustav van der Merwe

signature.asc

Antonio Salazar Cardozo

unread,
Aug 18, 2015, 4:09:32 PM8/18/15
to Lift, gv...@gvdm.me
No, it's not valid. It's a valid JavaScript object, but not valid JSON. JSON is very
strict: keys must be in double quotes, values that are strings must likewise be in
double quotes.

So this would be valid JSON:

{"stamp":"XmooaL03kY1V1nk nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA==","necessary":true,"preferences":true,"statistics":false,"marketing":false}

Thanks,
Antonio

Daniel Johannsen

unread,
Aug 21, 2015, 8:32:46 AM8/21/15
to Lift, gv...@gvdm.me
Cookiebot.com is using this format in the cookie named "cookieconsent" to store the website visitor's current cookie consent state. The reason for the used format is to support the Safari browser which still uses the original Netscape cookie specification instead of the newer RFC 6265 standard. The Netscape cookie spec does not allow valid JSON, but the cookie value can easily be converted to valid JSON using regular expressions:

PHP example:
$cookie_value = urldecode("{stamp:'XmooaL03kY1V1nk+nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA=='%2Cnecessary:true%2Cpreferences:true%2Cstatistics:false%2Cmarketing:false}");
$valid_php_json = preg_replace('/\s*:\s*([a-zA-Z0-9_]+?)([}\[,])/', ':"$1"$2', preg_replace('/([{\[,])\s*([a-zA-Z0-9_]+?):/', '$1"$2":', str_replace("'", '"',stripslashes($cookie_value))));

C# example:
String CookieValue = HttpUtility.UrlDecode("{stamp:'XmooaL03kY1V1nk+nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA=='%2Cnecessary:true%2Cpreferences:true%2Cstatistics:false%2Cmarketing:false}");
String ValidJSONString = Regex.Replace(Regex.Replace(CookieValue.Replace("'", "\""), @"([{\[,])\s*([a-zA-Z0-9_]+?):", "$1\"$2\":"), @"\s*:\s*([a-zA-Z0-9_]+?)([}\[,])", ":\"$1\"$2");

In .NET 3.5 or newer the cookie value can be deserialized directly using the JavaScriptSerializer of System.Web.Script.Serialization:

C# example:
String CookieValue = HttpUtility.UrlDecode("{stamp:'XmooaL03kY1V1nk+nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA=='%2Cnecessary:true%2Cpreferences:true%2Cstatistics:false%2Cmarketing:false}");
JavaScriptSerializer CookieConsentSerializer = new JavaScriptSerializer();
dynamic CookieConsent = CookieConsentSerializer.Deserialize<object>(CookieValue);

Visual Basic example:
Dim CookieValue As [String] = HttpUtility.UrlDecode("{stamp:'XmooaL03kY1V1nk+nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA=='%2Cnecessary:true%2Cpreferences:true%2Cstatistics:false%2Cmarketing:false}")
Dim CookieConsentSerializer As New JavaScriptSerializer()
Dim CookieConsent As dynamic = CookieConsentSerializer.Deserialize(Of Object)(CookieValue)

Cheers,
Daniel
Reply all
Reply to author
Forward
0 new messages