title: What difference occurs in my JSON to have one work, and the other not?
body: <p>I have a large amount of JSON embedded into my page via Ruby on Rail's HAML, that on load, gets injected into a variety of javascript views that display analytics.</p>
  
  <p>As so :</p>
  
  <pre><code>:javascript
     $(function() {
       var options = '#{raw admin_analytics.to_json.gsub(/'/,'`') }';
     });
  </code></pre>
  
  <p>This was working fine for nearly two years. Until suddenly! This created a JSON parsing error which seemed to fail right around where an apostraphe was being used in one of the values.</p>
  
  <p>We then cycled through a variety of different implementations :</p>
  
  <pre><code>     var options = '#{admin_analytics.to_json }';
       var options = '#{html_escape(admin_analytics.to_json) }';
       var options = '#{j(admin_analytics.to_json) }';
       var options = '#{admin_analytics.to_json.html_safe}';
  </code></pre>
  
  <p>And several others flavors, but everything returned the same error, and everything seemed to fail around the same apostraphe. We then tried changing that specific data attribute manually, but it would nonetheless, find some other apostraphe and fail there.</p>
  
  <p>I should add as well, that doing something like <code>html_escape</code> worked fine in console, but by the time it was embedded in the HTML, the parts that would break, were not encoded. They remained apostraphes, and therefore, broke the JSON.</p>
  
  <p>Finally, I managed to solve the entire debacle by just writing this :</p>
  
  <pre><code>:javascript
    $(function() {
      $.getJSON('/settings/load_analytics', function(data) {
        window.MyCustomClass.init_admin_analytics(data);  
      })
    });
  </code></pre>
  
  <p>Which worked! </p>
  
  <p>My question is.. why did that work? Is embedding JSON intrinsically unsafe? Is there a possibility, I missed some more obvious way to embed JSON?</p>