Sql multiple colums to json, it possible?

I have a table “x”, which I must cross with “y”.

But I need the opening generated by table “y” to be in one row. For this, I need to put together a json.

I can’t find an option to do something like that, I was thinking of building something recursive, but can’t find any option.

Is it possible to build something like that?

example

team team careers Tracks json (result grouped)
mercedes mercedes 1 Sakhir [{“mercedes”:[{“careers”:1, “tracks”:”Sakhir”},{“careers”:1, “tracks”:”Autódromo Enzo e Dino Ferrari”}]
alpin mercedes 2 Autódromo Enzo e Dino Ferrari
haas leftjoin haas 1 Autódromo Internacional do Algarve [{“haas”:[{“careers”:1, “tracks”:”Autódromo Internacional do Algarve”},{“careers”:2, “tracks”:”Barcelona-Catalunya},{“careers”:3, “tracks”:”Mónaco”}]
mclaren haas 2 Barcelona-Catalunya
alfa romeo haas 3 Mónaco

image