Fellow Stranger
Fellow Stranger

Reputation: 34103

Convert a Ruby Hash into JSON (without escape characters)

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?

Upvotes: 29

Views: 28298

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51181

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.

Upvotes: 67

Related Questions