Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Issuing on PostgresSQL 9.2 the following:

CREATE TABLE test (j JSON, ja JSON[]);
INSERT INTO test (j) VALUES('{"name":"Alex", "age":20}' ); -- Works FINE
INSERT INTO test (ja) VALUES( ARRAY['{"name":"Alex", "age":20}', '{"name":"Peter", "age":24}'] ); -- Returns ERROR

The first insert works fine. The second insert returns error: column "ja" is of type json[] but expression is of type text[]

I can cast type to prevent the error:

INSERT INTO test(ja) VALUES( CAST (ARRAY['{"name":"Alex", "age":20}', '{"name":"Peter", "age":24}'] as JSON[]) ); -- Works FINE

My question is if there is a way to avoid casting?

share|improve this question
up vote 4 down vote accepted
insert into test(ja) values
('{"{\"name\":\"alex\", \"age\":20}", "{\"name\":\"peter\", \"age\":24}"}');

Or if don't mind just a bit of casting with the advantage of a clearer string:

insert into test(ja) values
(array['{"name":"alex", "age":20}'::json, '{"name":"peter", "age":24}'::json]);
share|improve this answer
    
Thanks for the reply, especially the second form. – rlib Aug 7 '13 at 12:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.