0

I have been trying to figure out parsing a hashtree from rails backend as JSON in javascript. Have tried several solutions on stackoverflow and others including:

The object I am trying to parse:

{  
   "name"=>"map",
   "children"=>[  
      {  
         "name"=>"servicegroup1",
         "children"=>[  
            {  
               "name"=>"service1",
               "size"=>200,
               "id"=>10,
               "tooltip"=>"      <h3 id=\"status_tooltip_heading\">Service status<\/h3>\n      \n      <table id=\"status_tooltip_grid\">      \n        <tbody>\n          <tr>\n            <td>Status:<\/td>\n            <td>Unknown (an eternity ago)<\/td>          \n          <\/tr>\n          <tr>\n            <td>HTTP code:<\/td>\n            <td>\"N/A\"<\/td>          \n          <\/tr>                \n          <tr>\n            <td>Response time:<\/td>\n            <td>\"N/A\"<\/td>          \n          <\/tr>\n            <td>Event message:<\/td>\n            <td>Not pinged since eternity<\/td>          \n          <\/tr>\n        <\/tbody>\n      <\/table>\n"
            },
            {  
               "name"=>"service2",
               "size"=>200,
               "id"=>11,
               "tooltip"=>"      <h3 id=\"status_tooltip_heading\">Service status<\/h3>\n      \n      <table id=\"status_tooltip_grid\">      \n        <tbody>\n          <tr>\n            <td>Status:<\/td>\n            <td>Unknown (an eternity ago)<\/td>          \n          <\/tr>\n          <tr>\n            <td>HTTP code:<\/td>\n            <td>\"N/A\"<\/td>          \n          <\/tr>                \n          <tr>\n            <td>Response time:<\/td>\n            <td>\"N/A\"<\/td>          \n          <\/tr>\n            <td>Event message:<\/td>\n            <td>Not pinged since eternity<\/td>          \n          <\/tr>\n        <\/tbody>\n      <\/table>\n"
            },
            ...
         ]
      },
      ...
   ]
}

Here is how I am currently trying to parse it as JSON in javascript, having tried the techniques in the above links.

<script>jsonObj = JSON.parse('<%==(raw @map.to_s).gsub("=>",":").gsub("</","<\\/") %>')</script>

Have been stuck on this for hours now. I would really appreciate any insight you could give me on this. Also, the rails version is 4.1.2

1 Answer 1

1

It is enough to use:

<script>jsonObj = <%=raw ( @map.to_json) %></script>
5
  • undefined method `gsub' for #<Hash:0x007f7d2000d558> Tried j(@map.to_s) which is returning a string like: {\&quot;name\&quot;=&gt;\&quot;map\&quot;, \&quot;children\&quot;=&gt;[{\&quot;name\&quot;...
    – mohitt
    Nov 13, 2014 at 10:19
  • json_escape(@map) gives me a string like {&quot;name&quot;=\u003e&quot;map&quot;, &quot;children&quot;=\u003e[{&quot;
    – mohitt
    Nov 13, 2014 at 10:27
  • Well, I didn't tested previous answer, but now it is fine. There's no need to use JSON.parse.
    – blelump
    Nov 13, 2014 at 10:53
  • yeah I ended up doing the same and have been able to pin-point where exactly the problem is. I am updating it in my question.
    – mohitt
    Nov 13, 2014 at 11:11
  • Thanks a ton @blelump! this method worked perfectly once I changed the quotes in my HTML content to single quotes and removed newlines! I think you should add it to your answer for future reference.
    – mohitt
    Nov 13, 2014 at 11:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.