Postgres array of JSON avoid casting -
issuing on postgressql 9.2 following:
create table test (j json, ja json[]); insert test (j) values('{"name":"alex", "age":20}' ); -- works fine insert test (ja) values( array['{"name":"alex", "age":20}', '{"name":"peter", "age":24}'] ); -- returns error
the first insert works fine. second insert returns error: column "ja" of type json[] expression of type text[]
i can cast type prevent error:
insert test(ja) values( cast (array['{"name":"alex", "age":20}', '{"name":"peter", "age":24}'] json[]) ); -- works fine
my question if there way avoid casting?
insert test(ja) values ('{"{\"name\":\"alex\", \"age\":20}", "{\"name\":\"peter\", \"age\":24}"}');
to avoid confuse escaping cast each string:
insert test(ja) values (array['{"name":"alex", "age":20}'::json, '{"name":"peter", "age":24}'::json]);
or cast array itself:
insert test (ja) values (array['{"name":"alex", "age":20}', '{"name":"peter", "age":24}']::json[]);
Comments
Post a Comment