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

Community
  • 1
  • 1
mohitt
  • 440
  • 5
  • 9

1 Answers1

1

It is enough to use:

<script>jsonObj = <%=raw ( @map.to_json) %></script>
blelump
  • 3,233
  • 1
  • 16
  • 20
  • undefined method `gsub' for # Tried j(@map.to_s) which is returning a string like: {\"name\"=>\"map\", \"children\"=>[{\"name\"... – mohitt Nov 13 '14 at 10:19
  • json_escape(@map) gives me a string like {"name"=\u003e"map", "children"=\u003e[{" – mohitt Nov 13 '14 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 '14 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 '14 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 '14 at 11:34