pgo icon indicating copy to clipboard operation
pgo copied to clipboard

parse json_agg as json

Open benbro opened this issue 1 year ago • 1 comments

Is it possible to parse the json and jsob types? Currenty pgo returns the column as binary. OTP 27 will have a json module so it might make sense.

CREATE TEMPORARY TABLE j (
    id SERIAL PRIMARY KEY,
    name varchar(10)
);

INSERT INTO j (name) VALUES ('n1');
INSERT INTO j (name) VALUES ('n2');

SELECT json_agg(json_build_array(id, name)) FROM j;
pgo:query(<<"SELECT json_agg(json_build_array(id, name)) FROM j;">>).
#{command => select,
  rows => [{<<"[[1, \"n1\"], [2, \"n2\"]]">>}],
  num_rows => 1}

I can't use array_agg instead of json_agg because arrays support only single data type.

SELECT array_agg(ARRAY[id, name]) FROM j;
ERROR:  ARRAY types integer and character varying cannot be matched

benbro avatar May 13 '24 22:05 benbro

oooh, I hadn't even thought of that use case for otp-27's json yet!

tsloughter avatar May 14 '24 09:05 tsloughter