3

If I want to get the list of json object's keys aggregated in a json array in a single query. Here is what I am trying and it gives me an error:

Postgres version : 9.3

postgres=# create temporary table t_test ( 
postgres(# id integer,
postgres(# options json
postgres(# );
CREATE TABLE
postgres=# insert into t_test values (1, '{"x": 1, "y": 2}');
INSERT 0 1
postgres=# select * from t_test ;
 id |     options      
----+------------------
  1 | {"x": 1, "y": 2}
(1 row)

postgres=# select json_object_keys(options) from t_test ;
 json_object_keys 
------------------
 x
 y
(2 rows)

postgres=# select json_agg(json_object_keys(options)) from t_test ;
ERROR:  set-valued function called in context that cannot accept a set

1 Answer 1

1
select json_agg(o)
from (
    select json_object_keys(options) as o
    from t_test
) s
;
  json_agg  
------------
 ["x", "y"]
Sign up to request clarification or add additional context in comments.

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.