12

In postgresql, allowed array types are of integers and text.

myarray text[];     // ['a','b','c']
myarray integer[];  // [1,2,3]

But how can I create array of objects like -

[{'dsad':1},{'sdsad':34.6},{'sdsad':23}]

I don't want to use JSON type. Using array type I need to store the array of objects.

7 Answers 7

15

If you're running Postgres 9.2+, you can use the JSON type.

For example, we could do

create table jsontest (id serial primary key, data json);
insert into jsontest (data) values ('[{"dsad":1},{"sdsad":34.6},{"sdsad":23}]');

And query the data with

select data->1 from jsontest;
{"sdsad":34.6}
Sign up to request clarification or add additional context in comments.

1 Comment

actually you can get JSON as an extension for Pg 9.1 too. It just doesn't come out of the box.
6

You say:

I dont want to use JSON type

but you cannot use an ordinary array, as PostgreSQL arrays must be of homogenous types. You can't have a 2-dimensional array of text and integer.

What you could do if you don't want to use json is to create a composite type:

CREATE TYPE my_pair AS (blah text, blah2 integer);

SELECT ARRAY[ ROW('dasd',2), ROW('sdsad', 34.6), ROW('sdsad', 23) ]::my_pair[]

which will emit:

                 array                  
----------------------------------------
 {"(dasd,2)","(sdsad,35)","(sdsad,23)"}
(1 row)

If you don't want that, then json is probably your best bet. Or hstore:

SELECT hstore(ARRAY['a','b','c'], ARRAY[1,2,3]::text[])

            hstore            
------------------------------
 "a"=>"1", "b"=>"2", "c"=>"3"
(1 row)

Comments

2

JSON is your preferred answer, but more info as to why.

You can do something like:

SELECT array_agg(v) 
  FROM mytable v;

However you get something that looks like this:

{"(dsad,1)","(sdsad,34.6)","(""sdsad,var"",23)"}

It is then up to you to know how to decode this (i.e. column order). This is possible to do programmatically but is much easier with JSON.

Comments

2

It's hacky, but what about using an array for each property in the object (and its corresponding scalar type). If you have a data model layer in your get/read you could put the arrays "back together" into an array of objects and in your save method you would break you objects apart into synchronized arrays. This might be complicated by your example of each object not having the same properties; IDK how you'd store undefined for a property unless you're willing for null to be the same semantically.

Comments

1

It's not entirely clear if you mean json:

# select '[{"dsad":1},{"sdsad":34.6},{"sdsad":23}]'::json;
                   json                   
------------------------------------------
 [{"dsad":1},{"sdsad":34.6},{"sdsad":23}]
(1 row)

Or an array of json:

# select array['{"dsad":1}', '{"sdsad":34.6}', '{"sdsad":23}']::json[];
                        array                         
------------------------------------------------------
 {"{\"dsad\":1}","{\"sdsad\":34.6}","{\"sdsad\":23}"}
(1 row)

Or perhaps hstore? If the latter, it's only for key-values pairs, but you can likewise use an array of hstore values.

Comments

1

You can do something like:

SELECT JSON_AGG(v) FROM mytable v;

However you get something that looks like this:

["00000000-0000-0000-0000-000000000001","00000000-0000-0000-0000-000000000002", "00000000-0000-0000-0000-000000000003"]

exemple :

SELECT title, (select JSON_AGG(v.video_id) FROM videos v WHERE v.channel_id = c.channel_id) AS videos FROM channel AS c

Comments

0

Use text[] myarray insted of myarray text[].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.