After creating a hash structure in Ruby like this:
Track_list = {:track1=>{:url=>"https://open.spotify.com/track/2Oehrcv4Kov0SuIgWyQY9e", :name=>"Demons"},
:track2=>{:url=>"https://open.spotify.com/track/0z8yrlXSjnI29Rv30RssNI", :name=>"Shots - Broiler Remix"},
:track3=>{:url=>"https://open.spotify.com/track/6Ep6BzIOB9tz3P4sWqiiAB", :name=>"Radioactive"},
:track4=>{:url=>"https://open.spotify.com/track/3I05foFixB2sSZvV5Ppty8", :name=>"Blank Space/Stand By Me - Medley / Live From Spotify London"},
:track5=>{:url=>"https://open.spotify.com/track/4G8gkOterJn0Ywt6uhqbhp", :name=>"Radioactive"}}
I attempted to convert this hash into JSON format for use in my JavaScript file by executing the following code in my controller:
@tl = track_list.as_json
# The resulting JSON output looks like this:
# {"track1"=>{"url"=>"https://open.spotify.com/track /2Oehrcv4Kov0SuIgWyQY9e", "name"=>"Demons"},
# "track2"=>{"url"=>"https://open.spotify.com/track/0z8yrlXSjnI29Rv30RssNI", "name"=>"Shots - Broiler Remix"},
# "track3"=>{"url"=>"https://open.spotify.com/track/6Ep6BzIOB9tz3P4sWqiiAB", "name"=>"Radioactive"},
# "track4"=>{"url"=>"https://open.spotify.com/track/3I05foFixB2sSZvV5Ppty8", "name"=>"Blank Space/Stand By Me - Medley / Live From Spotify London"},
# "track5"=>{"url"=>"https://open.spotify.com/track/4G8gkOterJn0Ywt6uhqbhp", "name"=>"Radioactive"}}
However, upon attempting to print out the JSON object in my JS file, I encountered an issue where quotes were not correctly escaped:
console.log("<%= @tl %>");
"{"track1":{"url":"https://open.spotify.com/track/2Oehrcv4Kov0SuIgWyQY9e","name":"Demons"} ..."
Subsequently, when attempting to parse the string to JSON using `JSON.parse`, it did not work and trying to use `JSON.generate` also did not resolve the issue of unescaped quotes.
If anyone has a solution to this problem, your guidance would be greatly appreciated. Thank you!