0

In PHP I have an array which I encode with JSON and then I turn into a cookie.

<?php 

    $array = array("name" => "bob", "age" => "20");
    $arrayJSON = json_encode($array);
    setcookie("name", $arrayJSON, time() + (86400 * 33), "/");

?>

I'm now trying to do the same thing with Javascript. What is the Javascript equivalent of json_encoding a Javascript array and then setting it to a cookie? Here is what I have so far:

<script>

    var array = new Array();
    array["name"] = "bob";
    array["age"] = "20";
    // Need help here
    array = *javascript_json_encode*(array); // Do the Javascript equivalent of PHP's json_encode();
    document.cookie = "name="+array;

</script>

I'm trying to do this so that the format of the Javascript cookie is the same format as the PHP cookie. I need the Javascript cookie format to be as follows when you enter "document.cookie" in the browser console.

I need to turn "name: bob" and "age: 20" into %7B%22name%22%3A%22bob%22%2C%22age%22%3A%2220%22%7D". Don't care how I do it.

  • dupe of https://stackoverflow.com/questions/4162749/convert-js-object-to-json-string – CertainPerformance Feb 19 '21 at 22:54
  • I think I need something different. JSON.stringify() is for turning a JSON object into a string. I'm trying to turn a Javascript associative array into a JSON string – Stack Attack Feb 19 '21 at 23:00
  • 1
    JS has no such thing as an "associative array". There are *objects* and *arrays* in JS. If you want key-value pairs, use an object, not an array. – CertainPerformance Feb 19 '21 at 23:00
  • Console.log(JSON.stringify(array)); returns "[]", not "%7B%22name%22%3A%22bob%22%2C%22age%22%3A%2220%22%7D"" – Stack Attack Feb 19 '21 at 23:01
  • 2
    If you want key-value pairs, use an object, not an array. – CertainPerformance Feb 19 '21 at 23:01
  • I need to turn "name: bob" and "age: 20" into %7B%22name%22%3A%22bob%22%2C%22age%22%3A%2220%22%7D". Don't care how I do it – Stack Attack Feb 19 '21 at 23:02
  • 1
    Like I said, if you want key-value pairs, use an object, not an array. – CertainPerformance Feb 19 '21 at 23:02
  • @StackAttack I think you're confused by the languages. JS is _not_ PHP! PHP uses associative arrays to implement both arrays and what are essentially hash maps, keyed dictionary structures. JS does not--it uses objects and maps to implement associative arrays. There's a totally different structure, a normal array, that does not have string keys and is used for sequential integer numbers. `var array = new Array(); array["name"] = "bob"` is virtually useless and does not do what you think. This should be `var obj = {name: "bob"}` which creates an _object_, the basic key-value structure in JS. – ggorlen Feb 19 '21 at 23:04
  • Ok, good to know. Thanks ggorlen. But how do I take var obj = {name: "bob", age: "20"} and encode it in the following format: %7B%22name%22%3A%22bob%22%2C%22age%22%3A%2220%22%7D"? – Stack Attack Feb 19 '21 at 23:08
  • `encodeURIComponent(JSON.stringify(obj))` – ggorlen Feb 19 '21 at 23:09

1 Answers1

2

Converting my comments to an answer. PHP treats arrays differently than JS--it uses one structure for both sequentially-increasing integer structures and for key-value pair dictionary/hashmap-like structures.

JS arrays do not accept anything but sequential integer numbers, so const a = new Array(); a["foo"] = "bar" is fundamentally wrong--there's almost never any use case to set these properties on array objects, even if it seems to work.

If you want a key-value pair structure, use an object or a Map. If you want to stringify it to JSON, use JSON.stringify. If you want to encode that as a URI component, use encodeURIComponent.

Putting it all together:

const obj = {name: "bob", age: 20};
const json = JSON.stringify(obj);
const encoded = encodeURIComponent(json);

console.log(obj);
console.log(json);
console.log(encoded);
ggorlen
  • 44,755
  • 7
  • 76
  • 106