I have following hash in my controller:
@suggested_places = {
"0": "Barcelona",
"1": "Madrid",
"2": "Valencia"
}
I transform it into a json:
@suggested_places_json = @suggested_places.to_json
Here is the output:
=> "{\"0\":\"Barcelona\",\"1\":\"Madrid\",\"2\":\"Valencia\"}"
In my file.js.erb, I want to play with the information. I want to parse the json into a JavaScript object. I do:
const json = "<%= @suggested_places_json %>";
console.log(json);
With following result:
{"0":"Barcelona","1":"Madrid","2":"Valencia"}
Why do I have the ugly " and not the json that I had in the backend? A json is a string, it should keep its form.
If I try to parse it:
console.log(JSON.parse(json));
I have following result:
Uncaught SyntaxError: Unexpected token & in JSON at position 1
at JSON.parse (<anonymous>)
at <anonymous>:6:18
at processResponse (application-50000f29e1bf2752ae7e56dda39f10dd0f67a47aea5c3f07ed0d933f3e1a256a.js:268)
at application-50000f29e1bf2752ae7e56dda39f10dd0f67a47aea5c3f07ed0d933f3e1a256a.js:196
at XMLHttpRequest.xhr.onreadystatechange (application-50000f29e1bf2752ae7e56dda39f10dd0f67a47aea5c3f07ed0d933f3e1a256a.js:251)
Could someone help me find out where I am doing the mistake? What am I missing?