0

I am using an API whose reply is in form of Hashie::Rash. an example:

country = #<Hashie::Rash confidence="99" geoname_id=3175395 iso_code="IT" names=#<Hashie::Rash de="Italien" en="Italy" es="Italia" fr="Italie" ja="イタリア共和国" pt_br="Itália" ru="Италия" zh_cn="意大利">>

country.class = Hashie::Rash

I want to convert this to Json, to look like

{"iso_code":"IT","names": {"pt_br":"Itália","es":"Italia","ru":"Италия","en":"Italy","zh_cn":"意大 利","fr":"Italie","de":"Italien","ja":"イタリア共和 国"},"confidence":"99","geoname_id":3175395}

when I try using to_json(), it produces this:

"{\"iso_code\":\"IT\",\"names\":{\"pt_br\":\"Itália\",\"es\":\"Italia\",\"ru\":\"Италия\",\"en\":\"Italy\",\"zh_cn\":\"意大利\",\"fr\":\"Italie\",\"de\":\"Italien\",\"ja\":\"イタリア共和国\"},\"confidence\":\"99\",\"geoname_id\":3175395}"

whose class is a string.

How can I convert it to a JSON or Hash form??. Thanks

ben
  • 6,000
  • 5
  • 35
  • 42
  • The output of to_json is always a string. What output were you expecting? – Frederick Cheung Jan 05 '15 at 15:23
  • possible duplicate of [Ruby to\_json issue with error "illegal/malformed utf-8"](http://stackoverflow.com/questions/18067203/ruby-to-json-issue-with-error-illegal-malformed-utf-8) – fedorqui Jan 05 '15 at 15:25
  • I want it to be an hash. For example country.class = Hash, from the class Hashie::Rash. ..@fedorqui, this is not a duplicate question. they are not working with Hashie::Rash. – ben Jan 05 '15 at 15:32
  • If you want a hash, you don't want it to be JSON. It already is a hash, a [`Hashie::Rash`](https://github.com/intridea/hashie#rash) is just a souped-up `Hash` class. Try `country['confidence']` and you should get `99`. – Nick Veys Jan 05 '15 at 15:37
  • thanks nick for the suggestion but I know that it is possible, but I want to save it into a db after serializing or else by using postgres hstore variable. That is the reason I need it to be an hash. – ben Jan 05 '15 at 15:47
  • JSON in ruby is just a string. So if you want to store it in a db after serializing, `to_json` is the correct method to call, as it will store the JSON string. Then when you pull the field back out, you would just de-serialize it with `JSON.parse` – Eugene Jan 05 '15 at 16:21
  • I think that is working. But why can't serialize work? In my model I have serialize :country, Hash. And the table column is of type text. I am getting this error hashie::Rush to text error – ben Jan 05 '15 at 17:14

1 Answers1

1

I assume that you have to_json format data look like:

str =  "{\"iso_code\":\"IT\",\"names\":{\"pt_br\":\"Itália\",\"es\":\"Italia\",\"ru\":\"Италия\",\"en\":\"Italy\",\"zh_cn\":\"意大利\",\"fr\":\"Italie\",\"de\":\"Italien\",\"ja\":\"イタリア共和国\"},\"confidence\":\"99\",\"geoname_id\":3175395}"

then go to terminal and require json and parse it like bellow:

require 'json'
ob = JSON.parse(str)

Then you get json formatted output there and this will be look like:

{"iso_code"=>"IT", "names"=>{"pt_br"=>"Itália", "es"=>"Italia", "ru"=>"Италия", "en"=>"Italy", "zh_cn"=>"意大利", "fr"=>"Italie", "de"=>"Italien", "ja"=>"イタリア共和国"}, "confidence"=>"99", "geoname_id"=>3175395}
Naim Rajiv
  • 3,324
  • 2
  • 17
  • 23
  • I think that is working. But why can't serialize work? In my model I have serialize :country, Hash. And the table column is of type text. I am getting this error hashie::Rush to text error – ben Jan 05 '15 at 17:16
  • @ben it's may be new issue, it wasn't in the log :) anyway if it's seems work then you can accept my answer. BTW, you can paste log in the other place then i can take a look on this and let you know. – Naim Rajiv Jan 05 '15 at 17:42