Attempting to get a JSON response that is structured like this:
{
'news_source_1' : [{'headline': 'title'},{'headline': 'title'}],
'news_source_2' : [{'headline': 'title'},{'headline': 'title'}],
'news_source_3' : [{'headline': 'title'},{'headline': 'title'}]
}
The query calls a single table grouped by the news_source which is a column in the table.
My code groups by the news source but does not use the news source as a key:
SELECT array_to_json(array_agg(stories)) FROM stories GROUP BY source
Returns:
{
[{'headline': 'title'},{'headline': 'title'}],
[{'headline': 'title'},{'headline': 'title'}],
[{'headline': 'title'},{'headline': 'title'}]
}
Is it possible to use the news source column as the parent key? Not sure how to write this SQL query with the PG son syntax.
table
stories (
news_source,
headline
)