Ilya  Lapan Ilya Lapan - 1 year ago 106
JSON Question

Ruby On Rails: Convert PostgreSQL JSON to usual JSON

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

, 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 Source

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:


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download