I'm trying to get the JSON object from a JSON outputted string from a Rails app. Currently in JavaScript I'm doing:
data = "<%= @chromosomes.html_safe %>";
However, since there are quotes at the beginning and end of the JSON object, it is not being rendered as a JSON object. Instead it is doing something like
data = "[{"name":"YHet","organism_id":"4ea9b90e859723d3f7000037"}]"
Is there a way that I can remove the beginning and end quotes so that the object is treated as an array instead of a string?
I'm trying to get the JSON object from a JSON outputted string from a Rails app. Currently in JavaScript I'm doing:
data = "<%= @chromosomes.html_safe %>";
However, since there are quotes at the beginning and end of the JSON object, it is not being rendered as a JSON object. Instead it is doing something like
data = "[{"name":"YHet","organism_id":"4ea9b90e859723d3f7000037"}]"
Is there a way that I can remove the beginning and end quotes so that the object is treated as an array instead of a string?
Share Improve this question edited Apr 9, 2012 at 15:04 Phillip Whisenhunt asked Apr 9, 2012 at 14:58 Phillip WhisenhuntPhillip Whisenhunt 1,3052 gold badges18 silver badges30 bronze badges 4- I don't know what "html_safe" means exactly, but just as a note, it's not necessarily correct to use HTML-safe escapes ("<" for "<" etc) when you're dropping something directly into JavaScript source. – Pointy Commented Apr 9, 2012 at 15:04
-
@Pointy in this case it probably is correct to use
html_safe
, since he probably wants[{"name":"<script src='badwebsite./bad.js'></script>","organism_id":"..."}]
instead of[{"name":"<script src='badwebsite./bad.js'></script>","organism_id":"..."}]
. – Peter C Commented Apr 9, 2012 at 15:11 - @alpha123 yes sometimes it's correct to use HTML escapes, but not always; for example, if the "organism_id" can contain HTML characters, it may be wrong to replace them if that string is purely intended to work inside JavaScript code and never be injected into the HTML. – Pointy Commented Apr 9, 2012 at 15:21
-
@Pointy correct, although it looks like
organism_id
is a hash or GUID or something else not going to contain < or > or &, andname
probably will be injected into HTML at some point. I'd say escape. – Peter C Commented Apr 9, 2012 at 15:25
5 Answers
Reset to default 10Why don't you do:
data = <%= @chromosomes.html_safe %>;
Sidenote:
I hope you do something like:
@chromosomes = [{ name: "YHet", organism_id: "foo" }].to_json
If you are using jQuery you can do the following
var data = jQuery.parseJSON('[{"name":"YHet","organism_id":"4ea9b90e859723d3f7000037"}]');
Use JSON
object that is included in most of browsers or if you are using jQuery use $.jsonParse
method that try to use JSON
object if defined otherwise parse using eval
or some safer way.
On controller
@my_var = MyObj.find_by_id(4).to_json
On page in haml way.
var my_json = $.parseJSON("#{j @my_var}"); //used JQuery to parser JSON string
Use eval:
var dataObject = eval('(' + dataString + ')');