Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL treating TEXT as JSON

I have a TEXT column in my table (it's actually 'character varying'), but the data in the column is valid json. How can I access fields in this column using Postgres 9.3's new JSON functions?

What I basically want is a way to be able to do:

SELECT mycolumn->'myfield' from mytable;

Do I need to convert the entire column to the native JSON datatype, or is there an easy/efficient way to cast the cells?

like image 558
Mike Curtiss Avatar asked Jan 12 '23 01:01

Mike Curtiss


1 Answers

You need a type cast:

SELECT mycolumn::json->'myfield' FROM mytable;
like image 181
Erwin Brandstetter Avatar answered Jan 19 '23 11:01

Erwin Brandstetter