0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Wasn't 100% sure about PostgreSQL, but was looking at this like... this looks like Ruby rockets. Good stuff!
Yeah, Ruby's support for Postgres JSON columns is fantastic but also confusingly transparent if you're not expecting it.
Oh, it actually serializes it straight away. Nice!
Yeah I've heard good things about Arrays too. Might have to get PostgreSQL running over the weekend :)
Postgres ARRAY and HSTORE columns are great as well. The JSONB column is where it's at, though, it does everything those do, and way more. Try using Postgres 9.5 or better to get the latest JSON features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.