0

I am trying to save an array in cookies:

setCookie("a", JSON.stringify([{a:1},{a:2}]))

But seems that browser stores a decoded version of my string and when I try to retrieve it:

JSON.parse(getCookie("a"))

I get parsing error. What is the solution to solve this problem?

Hadi Ranjbar
  • 1,692
  • 4
  • 21
  • 44
  • See this https://stackoverflow.com/questions/2980143/i-want-to-store-javascript-array-as-a-cookie , I think it is the same question. – Arun Saini Oct 15 '19 at 14:29
  • 1
    There's no built-in `setCookie` in JavaScript, so you should let us know what that function does. – ceejayoz Oct 15 '19 at 14:29
  • 1
    As setCookie is not the native following link will help you to set/get cookie: https://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie – Arun Saini Oct 15 '19 at 14:31
  • @ArunKumarSaini I have written setCookie function myself and it works well. The problem is cookie is save encoded and starts with % while saving. – Hadi Ranjbar Oct 15 '19 at 18:52
  • @HadiRanjbar , Is setCookie function working fine with string values? Also, it would be helpful if you update the question by posting the setCookie function as well. – Arun Saini Oct 16 '19 at 05:07

3 Answers3

2

Here is how to create/get a cookie with array value in javascript

JSON encode it, effectively producing a string like "{name:'myname',age:'myage'}" which you put in a cookie, retrieve when needed and decode back into a JavaScript array/object.

Example - store array in a cookie:

var arr = ['foo', 'bar', 'baz'];
var json_str = JSON.stringify(arr);
createCookie('mycookie', json_str);

Later on, to retrieve the cookie's contents as an array:

var json_str = getCookie('mycookie');
var arr = JSON.parse(json_str);

Note: cookie functions are not native, taken from How do I create and read a value from cookie? , see below:

    var createCookie = function(name, value, days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    else {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
Arun Saini
  • 6,714
  • 1
  • 19
  • 22
0

Try to use createCookie and getCookie. It gives you a parser error because getCookie("a") returns undefined.

Patrick
  • 367
  • 1
  • 13
0

try to serialize the JSON data into single String and saving it in the cookie while read it back unserialize the String and convert it back to JSON string or JSON object

Karthick
  • 281
  • 2
  • 7