I have a table:
CREATE TABLE test (
item_id INTEGER NOT NULL,
item_name VARCHAR(255) NOT NULL,
mal_item_name VARCHAR(255),
active CHAR(1) NOT NULL,
data_needed CHAR(1) NOT NULL,
parent_id INTEGER);
The query:
select array_to_json(array_agg(row_to_json(t)))
from (select item_id as id,
item_name as text,
parent_id as parent,
(mal_item_name,data_needed) as data
from test) t
produces result:
[{"id":1,"text":"Materials","parent":0, "data": {"f1":null,"f2":"N"}}, {"id":2,"text":"Bricks","parent":1, "data":{"f1":null,"f2":"N"}}, {"id":3,"text":"Class(high)","parent":2, "data":{"f1":null,"f2":"Y"}}, {"id":4,"text":"Class(low)","parent":2, "data":{"f1":null,"f2":"Y"}}]
The original field names mal_item_name
and data_needed
are replaced with f1
and f2
.
How can I get a JSON with field names itself? Documentation says by creating a type for these two fields. Is there an alternative?