title: Parsing JSON string in Rails from Cookie generated by JavaScript
body: <p>I am trying to parse a JSON string that is stored inside a cookie value that my Rails code is calling.</p>
  
  <p>Rails is able to read the string up until the comma (',') that separates the two different key:value pairs in the string.</p>
  
  <p>JavaScript:</p>
  
  <pre><code>var value1 = "v1";
  var value2 = "v2";
  var obj = { key1: value1, key2: value2 };
  document.cookie = "cookiename="+JSON.stringify(obj);
  </code></pre>
  
  <p>Cookie:</p>
  
  <pre><code>Name: cookiename
  Content: {"key1":v1,"key2":v2}
  </code></pre>
  
  <p>Rails:</p>
  
  <pre><code>@cookievalue = cookies[:cookiename]
  </code></pre>
  
  <p>Rails when calling @cookievalue in an erb &lt;%= @cookievalue %> evaluates it as:</p>
  
  <pre><code>{"key1":v1
  </code></pre>
  
  <p>anything past the comma (',') that separates key1:v1,key2:v2 is missing.</p>
  
  <p>Any ideas?</p>
  
  <p>I tried this as straight text and it does the same thing with the first comma it encounters.</p>
  
  <p><strong>UPDATED</strong>
  Answered my own question below - needed to escape the comma separating the values using an encode() in JS.</p>