I tried to parse a simple JSON like that:
JSON.parse({"pong": "ok"})
and it failed
2.4.0 :014 > JSON.parse({"pong": "ok"})
TypeError: no implicit conversion of Hash into String
from (irb):14
What's wrong here ? Why should I convert to String ?
Another try, with OpenStruct this time:
2.4.0 :001 > pong = OpenStruct.new(pong: 'OK')
=> #<OpenStruct pong="OK">
2.4.0 :002 > JSON.parse(pong)
TypeError: no implicit conversion of OpenStruct into String
from (irb):2
The same ? Thank you
JSON.parse parses json and json means String:
JSON.parse('{"pong": "ok"}')
#⇒ {"pong"=>"ok"}
Also, you might parse json string into OpenStruct:
JSON.parse('{"pong":"ok"}', object_class: OpenStruct).pong
#⇒ "ok"
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