I have a table called games which holds information about a 'home team' and an 'away team'
there is a foreign key relationship in that games has a home_team_id and an away_team_id that refers to a team
My desired json output should look like this
{
"id":18203,
"date":"2022-10-22T18:00:00",
"away_team" : {
"team_id":24,
"abbr":"PHI"
},
"home_team" : {
"team_id":22,
"abbr":"NYK"
},
"home_team_id":22,
"away_team_id":24
}
however with this sql query
select row_to_json(t) from (
select * from games g
inner join teams home_team on home_team.id = g.home_team_id
inner join teams away_team on away_team.id = g.away_team_id
where g.day = '2022-10-22T00:00:00'
)t;
i get a flat json output with duplicate keys like this
{
"id":18203,
"date":"2022-10-22T18:00:00",
"team_id":24,
"abbr":"PHI"
"team_id":22,
"abbr":"NYK"
"home_team_id":22,
"away_team_id":24
}
how do I write my sql in order to get the json output that i want