Answer the question
In order to leave comments, you need to log in
How to get JSON object from PostgreSQL?
PostgreSQL 9.4
How to get data like this:
parentTableFirstProp: 'string',
parentToChildReference: [
{childTableFirstProp: 'another string'},
{childTableFirstProp: 'yet another string'}
}]
[{
parentTableFirstProp: 'string',
childTableFirstProp: 'another string',
},{
parentTableFirstProp: 'string',
childTableFirstProp: 'yet another string'
}]
Answer the question
In order to leave comments, you need to log in
select row_to_json(t)
from (
select "ParentTable"."parentTableFirstProp", (
select array_to_json(array_agg(row_to_json(child)))
from (
select "childTableFirstProp"
from "ChildTable"
where "ChildTable"."id"="ParentTable"."parentToChildReference"
) child
) as parentToChildReference
from "ParentTable"
) t
Something like this:
select row_to_json(j.*) from (
select 'string' as parentTableFirstProp, to_json(
(
select to_json(array_agg(r.*)) from (
select s as childTableFirstProp from (select unnest('{another string,yet another string}'::text[]) as s) _
)as r
)
) as parentToChildReference
) as j
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question