Ilya  Lapan Ilya Lapan - 10 days ago 6
JSON Question

Ruby On Rails: Convert PostgreSQL JSON to usual JSON

I have a JSON field in my PostgreSQL database. If I do

@profile.json
, then I will get something like:

{ {"name"=>"jhon", "degree"=>"12312"}, "1480103144467"=>{"name"=>"", "degree"=>""}}`


It has all the
=>
and other symbols, which I can not parse. How can I convert to normal format?

Answer

If you've declared your column of type json that's a signal to Rails to automatically serialize and decode your column on-demand, transparently. What you're seeing here is a traditional Ruby Hash structure, which is to be expected.

Inside the database itself it's stored as JSON.

If you need to re-emit this as JSON for whatever reason, like for an API, try this:

@profile.json.to_json

Calling your column something other than json is probably advisable, too.

Comments