The output of the select statement can be returned as JSON using the for json auto at the end of the query. The duplicates can be avoided using the proper join hints.