PostgreSQL: Using json_agg() aggregate table data into a JSON formatted array

Recently, PostgreSQL introduced JSON data type which is very powerful to manipulate JSON formatted data.
I have published a couple of articles on JSON Data Type.
You can start from here.

In this post, I am showing aggregation of table data into a JSON formatted array using json_agg().

Our requirement is to aggregate non json formatted data into JSON array and even some time also required to aggregate JSON formatted data.

I have prepared a small demonstration using json_agg().

First, create a table with sample JSON formatted data:

Aggregates JSON formatted data:

Aggregates non JSON formatted data:

Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

If you like this post, then please share it with others.
Please follow, I will share my experience towards the success of Database Research and Development Activity.

I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.

More from

Leave a Reply

Be the First to Comment!

Notify of