Convert string to JSON in Ruby

I read the data on the Internet, but it is in a string format. How can I make it return a JSON object.

Sample data:

text = '{"one":1,"two":2}' 

Conversion Example:

 data = JSON.parse(text).to_json 

But when I do this:

 puts data.class #=> String 
+5
source share
1 answer

Omit to_json : it converts the hash back to json! (JSON → Hash → JSON)

 require 'json' text = '{"one":1,"two":2}' data = JSON.parse(text) # <--- no `to_json` # => {"one"=>1, "two"=>2} data.class # => Hash 
+8
source

All Articles