i am struggling with storing a JSON object into Postgresql Array .. I did something like this --
CREATE TABLE rock_band
(
name text,
members VARCHAR(200) []
);
INSERT INTO rock_band VALUES ('Linkin Park','{"Name":"Chester" , "age" : "38"}');
ERROR: malformed array literal:
Any guidance/pointers on how we can store a json into Postgresql array and then append further json objects to the same array.
Also if we can store a JSON object in Postgresql Array , then can we store an HSTORE value.
--- UPDATE ---
Ok , with the help of one of the guys from heapanalytics , I am able to insert values as Array of JSON Objects
CREATE TABLE rock_band (
name TEXT,
members JSON[] <<--- This is changed now as compared to last one
);
INSERT INTO rock_band VALUES ('The Who', '{}');
To append to the array i did:
UPDATE rock_band SET members = array_append(members, '{"name":"Roger Daltry", "age":61}') WHERE name = 'The Who';
Then I get something like this --
select * from rock_band;
name | members
---------+----------------------------------------------------------------------------------
The Who | {"{\"name\":\"Roger Daltry\", \"age\":61}","{\"name\":\"Chester\", \"age\":38}"}
(1 row)
But now the question is how do i query it , something like --
test=# select members from rock_band where 'Chester' = ANY(members->'name');
ERROR: operator does not exist: json[] -> unknown
LINE 1: ...mbers from rock_band where 'Chester' = ANY(members->'name');
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Any suggestions ?