Обсуждение: convert from json to text[]

Поиск
Список
Период
Сортировка

convert from json to text[]

От
Mason Leung
Дата:
Hi,

I am running postgres 9.2.4 and 1 of my columns (column name is old_field) is type json.  When I select from this table, I get the following sample data

select old_field from table1;

[], 
['a', 'b']
['a']
[]

How do I change the data type from json to text[]?

I have tried

alter table table1 add column new_field text[] default '{}'::text[]"
UPDATE table1 SET new_field = {old_field} where old_field is not null"

Questions:
1) how to I change the data type from json to text[]
2) how to I select rows that has data and not [],

thanks
-m

Re: convert from json to text[]

От
Merlin Moncure
Дата:
On Wed, Jun 26, 2013 at 8:34 PM, Mason Leung <h2opbun@gmail.com> wrote:
> Hi,
>
> I am running postgres 9.2.4 and 1 of my columns (column name is old_field)
> is type json.  When I select from this table, I get the following sample
> data
>
> select old_field from table1;
>
> [],
> ['a', 'b']
> ['a']
> []
>
> How do I change the data type from json to text[]?
>
> I have tried
>
> alter table table1 add column new_field text[] default '{}'::text[]"
> UPDATE table1 SET new_field = {old_field} where old_field is not null"
>
> Questions:
> 1) how to I change the data type from json to text[]
> 2) how to I select rows that has data and not [],

json deserialization was heavily enhanced in the upcoming 9.3 release
which would make dealing with this triival.   for now, you have to
fire up pl/v8 or another pl with heavy json support or implement a
crude parser in sql :/


merlin


Re: convert from json to text[]

От
Andreas Kretschmer
Дата:
Merlin Moncure <mmoncure@gmail.com> wrote:

>
> json deserialization was heavily enhanced in the upcoming 9.3 release
> which would make dealing with this triival.   for now, you have to
> fire up pl/v8 or another pl with heavy json support or implement a
> crude parser in sql :/

http://www.pgxn.org/dist/json_enhancements/doc/json_enhancements.html



Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°