I have a hash
{:name =>"[email protected]"}
I am trying to convert this hash into the following JavaScript format.
{"name" : "[email protected]"}
I tried:
{:name=>"[email protected]"}.to_json
which gives the output:
"{\"name\":\"[email protected]\"}"
Parsing it with JSON gives:
JSON.parse({:name=>"[email protected]"}.to_json)
# => {"name"=>"[email protected]"}
"{\"name\":\"[email protected]\"}"is theinspectof the string, the default way strings are presented in console. The string content (escaped by backslashes byinspect, which is why you may have thought the result to be wrong) is:{"name":"[email protected]"}, exactly what you want. useputs {...}.to_jsoninstead of just{...}.to_jsonin console to verify this.