FOR JSON PATH vs FOR JSON AUTO SQL Server FOR JSON PATH vs FOR JSON AUTO SQL Server json json

FOR JSON PATH vs FOR JSON AUTO SQL Server


You need to use a subquery to produce a property that has a list for a value. Use an alias for the subquery that is the name of the property on the resulting JSON object.

This should do it for you:

SELECT    d.SCHOOL_WEBSITE AS 'websiteURL',    d.SCHOOL_EMAIL AS 'email ',    d.SCHOOL_TELEPHONE AS 'phone',    d.[Address 1] AS 'address.address1',    d.[Address 2] AS 'address.address2',    d.[Address 3] AS 'address.address3',    (        SELECT             [IndiUID] as 'name',            Value as 'value'         FROM [dbo].accreditations as ac        WHERE ac.SchoolCode = d.Code        FOR JSON PATH    ) AS accreditationsFROM dbo.DataValues dFOR JSON PATH;

(As a side note, you should discontinue use of the old implicit JOIN syntax.)