Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a Ruby Hash into JSON (without escape characters)

Tags:

I have a Hash:

my_hash = {"[email protected]"=>{"first"=>"Bob", "last"=>"Johnson"}, "[email protected]"=>{"first"=>"Lisa", "last"=>"Dell"}} 

When I try to serialize it with my_hash.to_json this is what I get:

"{\"[email protected]\":{\"first\":\"Bob\",\"last\":\"Johnson\"},\"[email protected]\":{\"first\":\"Lisa\",\"last\":\"Dell\"}}" 

How could I convert a Hash to JSON format without getting the escaping characters?

like image 774
Fellow Stranger Avatar asked Aug 27 '14 09:08

Fellow Stranger


Video Answer


1 Answers

These escape characters escape " in Ruby String (your my_hash.to_json output). If you do

puts my_hash.to_json 

you'll see that actually these escape characters aren't added to output string.

like image 82
Marek Lipka Avatar answered Sep 28 '22 01:09

Marek Lipka