Skip to content
Advertisement

PostgreSql format jsonb array

Can not find a proper query to format jsonb output in postgres I have a jonb column in a table.

How do I select the posts column to have just certain fields? Something like:

Any ideas?

PS: The version of postgres is the latest 12, 13, 14 …

Advertisement

Answer

You may considering using json_to_record_set to extract your desired columns for each array element in posts before aggregating the results by the user id to obtain the desired elements.

Query #1

id posts
1 [{“title”:””,”description”:””}]
2 [{“title”:””,”description”:””}]

View on DB Fiddle

or shorter

Query #2

id posts
1 [{“title”:””,”description”:””}]
2 [{“title”:””,”description”:””}]

View on DB Fiddle

or

Query #3


View on DB Fiddle

Let me know if this works for you.

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