Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Crie matrizes JSON aninhadas usando FOR JSON PATH


Você pode usar a seguinte consulta:
SELECT pr.person_id AS [person.id], pr.person_name AS [person.name],
    (
        SELECT pt.pet_id AS id, pt.pet_name AS name 
        FROM @Pets pt WHERE pt.pet_owner=pr.person_id 
        FOR JSON PATH
    ) AS [person.pet]
FROM @Persons pr 
FOR JSON PATH, ROOT('pet owners')

Para obter mais informações, consulte https://blogs.msdn.microsoft.com/sqlserverstorageengine/2015/10/09/returning-child-rows-formatted-as-json-in-sql-server-queries/