I'm new to json in postgres, so please don't throw anything at me. I'm trying to select data to jsonb object. I plan to use it for update from select. Right now it looks like this:
select to_jsonb(full_address) as full_address
from (
select "user".living_addr as living_id,
"user".living_addr_path as living_path,
null as living_house,
"user".address_level_id as registration_id,
"user".address_path as registration_path,
null as registration_house,
"user".work_addr as work_id,
"user".work_addr_path as work_path,
null as work_house,
public."user".user_id
from public."user"
) full_address
group by user_id, full_address
order by user_id
But I need also "living_id", "living_path", "living_house" to be into the object like this:
{
"living": {"id", "path", "house"}
}
and same for registration, work and learn.
Also, I don't need user_id in result object.
Tried to use subqueries like:
select to_jsonb(living) as living,
to_jsonb(registration) as registration,
...
from (
select "user".living_addr as id,
"user".living_addr_path as path,
null as house
from public."user"
) living,
(
select "user".address_level_id as id,
"user".address_path as path,
null as house
from public."user"
) registration
... etc
But, right now the result is wrong without grouping, and I can't find a clue how to use group by in this. There should be another simpler way I don't see.
The result object should look like this:
{
"work": {"id": ..., "path": [..., ...], "house": null},
"living": {"id": ..., "path": [..., ...], "house": null},
"registration": {"id": ..., "path": [..., ...], "house": null}
}
Is there a way to do this in postgres 9.6?