5

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?

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 ("&lt;" for "<" etc) when you're dropping something directly into JavaScript source. Commented Apr 9, 2012 at 15:04
  • @Pointy in this case it probably is correct to use html_safe, since he probably wants [{"name":"&lt;script src='badwebsite.com/bad.js'&gt;&lt;/script&gt;","organism_id":"..."}] instead of [{"name":"<script src='badwebsite.com/bad.js'></script>","organism_id":"..."}]. 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. 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 &, and name probably will be injected into HTML at some point. I'd say escape. Commented Apr 9, 2012 at 15:25

5 Answers 5

10

Why don't you do:

data = <%= @chromosomes.html_safe %>;

Sidenote:

I hope you do something like:

@chromosomes = [{ name: "YHet", organism_id: "foo" }].to_json
Sign up to request clarification or add additional context in comments.

4 Comments

I could, but JavaScript will not recognize the <%= :(. Thanks though.
This is clearly the best thing to do; if it's already JSON it should just be dropped directly into the JavaScript source without additional quotes. @PhillipWhisenhunt it's not JavaScript that recognizes the <%= %> here; it's your server-side framework. If it works inside the double-quotes now, it'll work without them.
@PhillipWhisenhunt um, that's the point. ERb goes in and processes everything starting with <% and ending with %> before the file gets sent to the client.
Correct. This is the best way to do it, removing the quotes fixed the issue. Whoops lol, @alpha123 had a major brain fart! Coding to early in the morning!
1

If you are using jQuery you can do the following

var data = jQuery.parseJSON('[{"name":"YHet","organism_id":"4ea9b90e859723d3f7000037"}]');

Comments

0

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.

Comments

0

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

Comments

-1

Use eval:

var dataObject = eval('(' + dataString + ')');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.