I have read data online, but it is in a string format. How can I make it so that it returns a JSON object.
Example data read:
text = '{"one":1,"two":2}'
Example conversion:
data = JSON.parse(text).to_json
But when I do:
puts data.class
#=> String
Omit to_json
: it will convert 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With