Skip to content
Advertisement

PostgreSQL json_build_object nested

First things first: Iโ€™m using PostgreSQL 11.6, compiled by Visual C++ build 1800, 64-bit. ๐Ÿ™‚

Im trying to create a JSON object directly from the database.

My desired result is

Imagine my tables like:

Now I create a query

This will get me as result, multiple rows with the desired result:

After this I can use json_agg() to create almost my desired result. The issue is that it will create

I would like to know if its possible to write a query to merge my created object into one json object like:

Thank you very much in advance for taking the time to read :)!

Advertisement

Answer

If I followed you correctly, you can add another level of aggregation and use json_object_agg():

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement