I have a SQL API to my Postgres database that returns JSON.
I have two tables: holidays
, which has a schema of name
, hd_id
. And photos
, which are photos take on that day. Its schema is url
, caption
, h_id
.
I'd like to create a nested json object like the one down below. The SQL I'm running is
SELECT holidays.name, holidays.h_id,
concat('[', group_concat(concat('{"src":"', photos.url, '","caption":"', photos.caption '"}', separater ','), ']') )
FROM holidays
INNER JOIN photos
ON holidays.h_id = photos.h_id
GROUP BY holidays.h_id
But that gives me the error "schema "photos" does not exist"
. Photos is a table, not a schema. I don't seem to be making the same mistake as this seemingly related question. I'm not sure how else to structure the JOIN.
Here's the desired JSON output.
[
{
name: 'Labor Day',
h_id: 'a1',
photos: [
{
src: 'url',
caption: 'text'
},
{
src: 'url',
caption: 'text'
}
]
},
{
name: 'MLK Day',
h_id: 'a2',
photos: [
{
src: 'url',
caption: 'text'
},
{
src: 'url',
caption: 'text'
}
]
}
]