How to convert a ruby hash object to JSON?
One of the numerous niceties of Ruby is the possibility to extend existing classes with your own methods. That's called "class reopening" or monkey-patching (the meaning of the latter can vary, though).
So, take a look here:
car = {:make => "bmw", :year => "2003"}
# => {:make=>"bmw", :year=>"2003"}
car.to_json
# NoMethodError: undefined method `to_json' for {:make=>"bmw", :year=>"2003"}:Hash
# from (irb):11
# from /usr/bin/irb:12:in `<main>'
require 'json'
# => true
car.to_json
# => "{"make":"bmw","year":"2003"}"
As you can see, requiring json
has magically brought method to_json
to our Hash
.
Ruby hash to json
If you convert hash into json then you can use .to_json
it will give you hash with "/"
, in your case you can not use .to_json
,Instead of this you may use .as_josn
,It will convert your hash without "/"
.
like:
<%= @my_hash.as_json %>
Ruby converting array to hash object to json
I suggest you use the method Array#product.
{ "cars": ["id"].product(ids).map { |k,v| { k=>v } } }
#=> {:cars=>[{"id"=>111}, {"id"=>333}, {"id"=>888}]}
or
{ "cars": ["id"].product(ids).map { |a| [a].to_h } }
#=> {:cars=>[{"id"=>111}, {"id"=>333}, {"id"=>888}]}
and then apply to_json
.
How to convert a hash to JSON without the converted string being full of backslashes
It seems like you are confused about the actual content of the string. The backslashes are part of the string representation, and are not actually in the string.
Take for example '"'
a single character "
. When you enter this into irb the output will be:
s = '"'
#=> "\""
From the result "\""
the starting "
and ending "
mark the begin and ending of the string while \"
is an escaped double quote, and represents a single "
. The reason this double quote is escaped is because you want Ruby to interpret it as an "
character, not as the end of the string.
You can view the actual contents of the string without escape characters by printing it to the console.
puts s
# "
#=> nil
Here you can see that there is no backslash in the contents of the string.
The same applies for your to_json
call, which returns a string:
data = {"a" => "b", "c" => "d", "e" => "f"}
json = data.to_json
#=> "{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"}"
puts json
# {"a":"b","c":"d","e":"f"}
#=> nil
Like you can see there are no backslashes in the contents of the string, only in the string representation.
How to convert ruby formatted json string to json hash in ruby?
You can try eval method on temp json string
Example:
eval(temp)
This will return following hash
{"accept"=>"*/*", "host"=>"localhost:4567", "version"=>"HTTP/1.1", "user_agent"=>"curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3", "http_token"=>"375fe428b1d32787864264b830c54b97"}
Hope this will help.
Thanks
Related Topics
How to Change My Ruby Version Using Rvm
Creating Microsoft Word (.Docx) Documents in Ruby
Where Is Ruby'S String Literal Juxtaposition Feature Officially Documented
Rails Activerecord: Find All Users Except Current User
Should Gemfile.Lock Be Included in .Gitignore
Sort an Array According to the Elements of Another Array
Backslashes in Single Quoted Strings Vs. Double Quoted Strings
How to Override a Setter Method in Ruby on Rails
What Does the Unary Question Mark () Operator Do
Is There Any Wisdom Behind "And", "Or" Operators in Ruby
Valid Email Address Regular Expression
Rails 4: Assets Not Loading in Production
How to Get a Query String from a Url in Rails
Regex for Checking the Last Character
Is There Any Difference Between the ':Key =≫ "Value"' and 'Key: "Value"' Hash Notations