Recently I had a text field being used to store JSON data from my web application. I hadn’t yet discovered the amazingness of the PostgreSQL JSONB datatype.
JSONB is an amazing datatype because Postgres understands how to query the structure and the keys and values can be indexed.
So if you have a text fields you want to convert to JSONB (provided the existing data is in properly formed JSON already) You can change the datatype and convert the existing data using this simple SQL command:
alter table tbl_name alter column col_name type jsonb using col_name::JSON;
You may be inclined to covert the text using the to_json function like: ‘USING to_json(col_name);’ – BUT DON’T DO THAT! What happens with that is you get a single string encapsulating the valid JSON, containing your JSON. Effectively a double-encoding bug. The former (casting the column to JSON is the correct way).