Returning child rows formatted as JSON in SQL Server queries

Published Mar 23 2019 01:52 PM 4,613 Views
Microsoft

First published on MSDN on Oct 09, 2015

In this post I will talk about one annoying thing – how to return a result set containing one to many relationships between tables (i.e. parent-child relationships such as company-products, person-address, sales order-items, etc.)?

 

Problem

I have relational structure with several one to many relationships (e.g. Person may have several phones, or several email addresses). In order to read Person and related information; In need to run several queries (e.g. one to return person info, another to return person phones, third to return person email addresses), or join tables and process one flat result set on the client side.

 

It would be nice if I could retrieve person information and all related information with a single SQL query.

 

Solution

Related information can be formatted as JSON arrays and returned as arrays in parent Person row. this way we need a single query to return all Person related information.

 

Scenario - Exporting complex relational data structures as JSON

 

In practice you will have one to many relationships in many cases, e.g. person can have many email addresses, many phones, etc. If you want to store these information in relational database, you would need to use something like a structure on the following figure:

 

 

Even for the simple structures such as phone numbers or emails that will be represented as arrays in C# or Java, you would need to create separate tables with foreign key relationships. Now if you try to join these tables you would need to use something like a following query:

 

SELECT Person.Person.BusinessEntityID, Person.Person.FirstName,
Person.Person.LastName, Person.EmailAddress.EmailAddress,
Person.PersonPhone.PhoneNumber, Person.PhoneNumberType.Name AS PhoneNumberType
FROM         Person.Person INNER JOIN
Person.EmailAddress ON Person.Person.BusinessEntityID = Person.EmailAddress.BusinessEntityID INNER JOIN
Person.PersonPhone ON Person.Person.BusinessEntityID = Person.PersonPhone.BusinessEntityID INNER JOIN
Person.PhoneNumberType ON Person.PersonPhone.PhoneNumberTypeID = Person.PhoneNumberType.PhoneNumberTypeID
WHERE Person.Person.BusinessEntityID = 274

 

If you run this query in AdventureWorks database, you will get information about person with email address and phone number:

 


BusinessEntityID


FirstName


LastName


EmailAddress


PhoneNumber


PhoneNumberType


274


Stephen


Jiang


stephen0@adventure-works.com


112-555-6207


Work

 

One row is returned because in AW database there is only one telephone number and email per person. Similar results will be returned if you query Sales.vSalesPerson view.

 

Now, since database schema allows us to add more than one address what would happen if you add another email address to Stephen?

 

INSERT INTO Person.EmailAddress(BusinessEntityID,EmailAddress)
VALUES (274,'stephen.jiang@outlook.com')

If you execute this query again, you will get the following result:

 


BusinessEntityID


FirstName


LastName


EmailAddress


PhoneNumber


PhoneNumberType


274


Stephen


Jiang


stephen0@adventure-works.com


112-555-6207


Work


274


Stephen


Jiang


stephen.jiang@outlook.com


112-555-6207


Work

 

 

 

Now let’s assume that someone adds Stephen’s home and cell phone numbers in the system. Query will now return the following results:

 


BusinessEntityID


FirstName


LastName


EmailAddress


PhoneNumber


PhoneNumberType


274


Stephen


Jiang


stephen0@adventure-works.com


112-555-6207


Work


274


Stephen


Jiang


stephen.jiang@outlook.com


112-555-6207


Work


274


Stephen


Jiang


stephen0@adventure-works.com


238-555-0197


Cell


274


Stephen


Jiang


stephen.jiang@outlook.com


238-555-0197


Cell


274


Stephen


Jiang


stephen0@adventure-works.com


817-555-1797


Home


274


Stephen


Jiang


stephen.jiang@outlook.com


817-555-1797


Home

 

 

 

This is a messy because now instead of a single row you are getting 6 rows because two email addressed are cross combined with three phone numbers.

 

Similar results will be returned if you query Sales.vSalesPerson view:

 

SELECT BusinessEntityID AS ID, FirstName, LastName, EmailAddress, PhoneNumber, PhoneNumberType
FROM Sales.vSalesPerson
WHERE BusinessEntityID = 274

Now you have two choices – handle these duplicates in client-side or try to return related data in single row. In this post I will show you how to use the second option with FOR JSON clause in SQL Server 2016.

Use Case 1: Formatting set of related rows as JSON array

 

Instead of joining related tables we can just attach related information as an array of records formatted as JSON array. We can select data from Person table, and add related email addresses as subquery formatted as JSON text:

 

SELECT     Person.Person.BusinessEntityID, Person.Person.FirstName, Person.Person.LastName,
     (SELECT Person.EmailAddress.EmailAddress
             FROM Person.EmailAddress
       WHERE Person.Person.BusinessEntityID = Person.EmailAddress.BusinessEntityID
        FOR JSON PATH) AS Emails
FROM         Person.Person
WHERE Person.Person.BusinessEntityID = 274

Since FOR JSON clause returns a single text field you can put it in any column:

 


ID


FirstName


LastName


Emails


274


Stephen


Jiang


[{"EmailAddress":"stephen0@adventure-works.com"},


{"EmailAddress":"stephen.jiang@outlook.com"}]

 

In this case we are returning hybrid result set - relational data from Person table are returned as columns, while related information from Person.emailAddress table are returned as an array of JSON objects.

 

You might notice that FOR JSON returns an array of key:value pairs even if we might like to have something simpler e.g. plain array of values instead of array of objects. In this case we can write simple T-SQL user defined function that removes keys from the array and return plain array:

 

SELECT     Person.Person.BusinessEntityID AS ID, Person.Person.FirstName, Person.Person.LastName,
dbo.ufnToRawJsonArray((SELECT Person.EmailAddress.EmailAddress
FROM Person.EmailAddress
WHERE Person.Person.BusinessEntityID = Person.EmailAddress.BusinessEntityID
FOR JSON PATH), 'EmailAddress') AS Emails
FROM         Person.Person
WHERE Person.Person.BusinessEntityID = 274

Result of this query might look like:

 


ID


FirstName


LastName


Emails


274


Stephen


Jiang


["stephen0@adventure-works.com","stephen.jiang@outlook.com"]

 

 

 

This is more readable format that array of key value pairs.

 

Use Case 2: Formatting related information from multiple child tables as JSON arrays

 

In the previous use case, I have returned content of one related child table as JSON array. We can also return related information both from emails and phone tables. Second set of related information would be returned as column level JSON expression:

SELECT     Person.Person.BusinessEntityID AS ID, Person.Person.FirstName, Person.Person.LastName,
      (SELECT Person.EmailAddress.EmailAddress
         FROM Person.EmailAddress
       WHERE Person.Person.BusinessEntityID = Person.EmailAddress.BusinessEntityID
       FOR JSON PATH) AS Emails,
       (SELECT Person.PersonPhone.PhoneNumber, Person.PhoneNumberType.Name AS PhoneNumberType
          FROM  Person.PersonPhone INNER JOIN
                Person.PhoneNumberType ON Person.PersonPhone.PhoneNumberTypeID = 
                                          Person.PhoneNumberType.PhoneNumberTypeID
            WHERE Person.Person.BusinessEntityID = Person.PersonPhone.BusinessEntityID
         FOR JSON PATH) AS Phones
FROM         Person.Person
WHERE Person.Person.BusinessEntityID = 274

 

In this case, we have returned a single row for a particular person and all related information are returned as an array of JSON object in the single line.

 


ID


FirstName


LastName


Emails


Phones


274


Stephen


Jiang


[


{"EmailAddress":"stephen0@adventure-works.com"},


{"EmailAddress":"stephen.jiang@outlook.com"}


]


[


{"PhoneNumber":"112-555-6207","PhoneNumberType":"Work"},


{"PhoneNumber":"238-555-0197","PhoneNumberType":"Cell"},


{"PhoneNumber":"817-555-1797","PhoneNumberType":"Home"}


]

 

 

 

You can easily unpack these arrays on the client side using some JSON deserializer such as JSON.NET. You might notice that we still have one array. The fact that we are adding new child items do not affects number of returned results.

 

Use Case 3: Formatting parent and child rows as JSON

 

Instead of hybrid result set, we can format all results as JSON (i.e. both parent Person columns and child rows). If you want to return all results as JSON there is even simpler syntax. FOR JSON AUTO will automatically indent related email addresses as it is shown in the following script:

 

SELECT     Person.Person.BusinessEntityID, Person.Person.FirstName, Person.Person.LastName, Person.EmailAddress.EmailAddress
FROM         Person.Person
    INNER JOIN Person.EmailAddress
          ON Person.Person.BusinessEntityID = Person.EmailAddress.BusinessEntityID
WHERE Person.Person.BusinessEntityID = 274
FOR JSON AUTO

Results might look like:

 

[

{ "ID":274,"FirstName":"Stephen","LastName":"Jiang",


"Person.EmailAddress":[


{"EmailAddress":"stephen0@adventure-works.com"},


{"EmailAddress":"stephen.jiang@outlook.com"}


]


}


]

 

This format is pure JSON and it can be used as a response of JSON web service.

 

Use Case 4: Returning complex JSON hierarchies

 

Finally we can format entire result-set as JSON by using FOR JSON cause on the main query:

SELECT     Person.Person.BusinessEntityID AS ID, Person.Person.FirstName, Person.Person.LastName,
       (SELECT Person.EmailAddress.EmailAddress
           FROM Person.EmailAddress
         WHERE Person.Person.BusinessEntityID = Person.EmailAddress.BusinessEntityID
         FOR JSON PATH) AS Emails,
        (SELECT Person.PersonPhone.PhoneNumber, Person.PhoneNumberType.Name AS PhoneNumberType
             FROM  Person.PersonPhone
             INNER JOIN Person.PhoneNumberType
                 ON Person.PersonPhone.PhoneNumberTypeID = 
                    Person.PhoneNumberType.PhoneNumberTypeID
           WHERE Person.Person.BusinessEntityID = Person.PersonPhone.BusinessEntityID
            FOR JSON PATH) AS Phones
FROM         Person.Person
WHERE Person.Person.BusinessEntityID = 274
FOR JSON PATH

 

Results will look like:

 


[


{


"ID":274,"FirstName":"Stephen","LastName":"Jiang",


"Emails":[


{"EmailAddress":"stephen0@adventure-works.com"},


{"EmailAddress":"stephen.jiang@outlook.com"}],


"Phones":[


{"PhoneNumber":"112-555-6207","PhoneNumberType":"Work"},


{"PhoneNumber":"238-555-0197","PhoneNumberType":"Cell"},


{"PhoneNumber":"817-555-1797","PhoneNumberType":"Home"}


]


}


]

 

FOR JSON clause enables you to format complex results and return them in more convenient format to the client. Note that you will need SQL Server 2016 CTP2 or higher for these code examples.

%3CLINGO-SUB%20id%3D%22lingo-sub-384482%22%20slang%3D%22en-US%22%3EReturning%20child%20rows%20formatted%20as%20JSON%20in%20SQL%20Server%20queries%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-384482%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Oct%2009%2C%202015%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EIn%20this%20post%20I%20will%20talk%20about%20one%20annoying%20thing%20%E2%80%93%20how%20to%20return%20a%20result%20set%20containing%20one%20to%20many%20relationships%20between%20tables%20(i.e.%20parent-child%20relationships%20such%20as%20company-products%2C%20person-address%2C%20sales%20order-items%2C%20etc.)%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1793451777%22%20id%3D%22toc-hId-1793451777%22%3EProblem%3C%2FH2%3E%0A%3CP%3EI%20have%20relational%20structure%20with%20several%20one%20to%20many%20relationships%20(e.g.%20Person%20may%20have%20several%20phones%2C%20or%20several%20email%20addresses).%20In%20order%20to%20read%20Person%20and%20related%20information%3B%20In%20need%20to%20run%20several%20queries%20(e.g.%20one%20to%20return%20person%20info%2C%20another%20to%20return%20person%20phones%2C%20third%20to%20return%20person%20email%20addresses)%2C%20or%20join%20tables%20and%20process%20one%20flat%20result%20set%20on%20the%20client%20side.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt%20would%20be%20nice%20if%20I%20could%20retrieve%20person%20information%20and%20all%20related%20information%20with%20a%20single%20SQL%20query.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--758705184%22%20id%3D%22toc-hId--758705184%22%3ESolution%3C%2FH2%3E%0A%3CP%3ERelated%20information%20can%20be%20formatted%20as%20JSON%20arrays%20and%20returned%20as%20arrays%20in%20parent%20Person%20row.%20this%20way%20we%20need%20a%20single%20query%20to%20return%20all%20Person%20related%20information.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-984105151%22%20id%3D%22toc-hId-984105151%22%3EScenario%20-%20Exporting%20complex%20relational%20data%20structures%20as%20JSON%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20practice%20you%20will%20have%20one%20to%20many%20relationships%20in%20many%20cases%2C%20e.g.%20person%20can%20have%20many%20email%20addresses%2C%20many%20phones%2C%20etc.%20If%20you%20want%20to%20store%20these%20information%20in%20relational%20database%2C%20you%20would%20need%20to%20use%20something%20like%20a%20structure%20on%20the%20following%20figure%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20679px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F97867i2C61706FC3CB0334%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEven%20for%20the%20simple%20structures%20such%20as%20phone%20numbers%20or%20emails%20that%20will%20be%20represented%20as%20arrays%20in%20C%23%20or%20Java%2C%20you%20would%20need%20to%20create%20separate%20tables%20with%20foreign%20key%20relationships.%20Now%20if%20you%20try%20to%20join%20these%20tables%20you%20would%20need%20to%20use%20something%20like%20a%20following%20query%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20Person.Person.BusinessEntityID%2C%20Person.Person.FirstName%2C%0APerson.Person.LastName%2C%20Person.EmailAddress.EmailAddress%2C%0APerson.PersonPhone.PhoneNumber%2C%20Person.PhoneNumberType.Name%20AS%20PhoneNumberType%0AFROM%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Person.Person%20INNER%20JOIN%0APerson.EmailAddress%20ON%20Person.Person.BusinessEntityID%20%3D%20Person.EmailAddress.BusinessEntityID%20INNER%20JOIN%0APerson.PersonPhone%20ON%20Person.Person.BusinessEntityID%20%3D%20Person.PersonPhone.BusinessEntityID%20INNER%20JOIN%0APerson.PhoneNumberType%20ON%20Person.PersonPhone.PhoneNumberTypeID%20%3D%20Person.PhoneNumberType.PhoneNumberTypeID%0AWHERE%20Person.Person.BusinessEntityID%20%3D%20274%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20run%20this%20query%20in%20AdventureWorks%20database%2C%20you%20will%20get%20information%20about%20person%20with%20email%20address%20and%20phone%20number%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EBusinessEntityID%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EFirstName%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3ELastName%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EEmailAddress%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EPhoneNumber%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EPhoneNumberType%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E274%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EStephen%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EJiang%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3Estephen0%40adventure-works.com%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E112-555-6207%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EWork%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20row%20is%20returned%20because%20in%20AW%20database%20there%20is%20only%20one%20telephone%20number%20and%20email%20per%20person.%20Similar%20results%20will%20be%20returned%20if%20you%20query%20Sales.vSalesPerson%20view.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%2C%20since%20database%20schema%20allows%20us%20to%20add%20more%20than%20one%20address%20what%20would%20happen%20if%20you%20add%20another%20email%20address%20to%20Stephen%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3EINSERT%20INTO%20Person.EmailAddress(BusinessEntityID%2CEmailAddress)%0AVALUES%20(274%2C'stephen.jiang%40outlook.com')%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EIf%20you%20execute%20this%20query%20again%2C%20you%20will%20get%20the%20following%20result%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EBusinessEntityID%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EFirstName%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3ELastName%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EEmailAddress%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EPhoneNumber%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EPhoneNumberType%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E274%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EStephen%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EJiang%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3Estephen0%40adventure-works.com%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E112-555-6207%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EWork%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E274%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EStephen%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EJiang%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3Estephen.jiang%40outlook.com%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E112-555-6207%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EWork%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20let%E2%80%99s%20assume%20that%20someone%20adds%20Stephen%E2%80%99s%20home%20and%20cell%20phone%20numbers%20in%20the%20system.%20Query%20will%20now%20return%20the%20following%20results%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EBusinessEntityID%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EFirstName%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3ELastName%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EEmailAddress%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EPhoneNumber%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EPhoneNumberType%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E274%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EStephen%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EJiang%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3Estephen0%40adventure-works.com%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E112-555-6207%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EWork%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E274%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EStephen%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EJiang%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3Estephen.jiang%40outlook.com%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E112-555-6207%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EWork%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E274%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EStephen%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EJiang%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3Estephen0%40adventure-works.com%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E238-555-0197%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3ECell%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E274%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EStephen%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EJiang%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3Estephen.jiang%40outlook.com%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E238-555-0197%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3ECell%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E274%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EStephen%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EJiang%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3Estephen0%40adventure-works.com%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E817-555-1797%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EHome%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E274%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EStephen%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EJiang%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3Estephen.jiang%40outlook.com%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E817-555-1797%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EHome%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20a%20messy%20because%20now%20instead%20of%20a%20single%20row%20you%20are%20getting%206%20rows%20because%20two%20email%20addressed%20are%20cross%20combined%20with%20three%20phone%20numbers.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESimilar%20results%20will%20be%20returned%20if%20you%20query%20Sales.vSalesPerson%20view%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20BusinessEntityID%20AS%20ID%2C%20FirstName%2C%20LastName%2C%20EmailAddress%2C%20PhoneNumber%2C%20PhoneNumberType%0AFROM%20Sales.vSalesPerson%0AWHERE%20BusinessEntityID%20%3D%20274%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3ENow%20you%20have%20two%20choices%20%E2%80%93%20handle%20these%20duplicates%20in%20client-side%20or%20try%20to%20return%20related%20data%20in%20single%20row.%20In%20this%20post%20I%20will%20show%20you%20how%20to%20use%20the%20second%20option%20with%20FOR%20JSON%20clause%20in%20SQL%20Server%202016.%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1764565315%22%20id%3D%22toc-hId--1764565315%22%3EUse%20Case%201%3A%20Formatting%20set%20of%20related%20rows%20as%20JSON%20array%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EInstead%20of%20joining%20related%20tables%20we%20can%20just%20attach%20related%20information%20as%20an%20array%20of%20records%20formatted%20as%20JSON%20array.%20We%20can%20select%20data%20from%20Person%20table%2C%20and%20add%20related%20email%20addresses%20as%20subquery%20formatted%20as%20JSON%20text%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Person.Person.BusinessEntityID%2C%20Person.Person.FirstName%2C%20Person.Person.LastName%2C%0A%20%20%20%20%20(SELECT%20Person.EmailAddress.EmailAddress%0A%20%20%20%20%20%20%20%20%20%20%20%20%20FROM%20Person.EmailAddress%0A%20%20%20%20%20%20%20WHERE%20Person.Person.BusinessEntityID%20%3D%20Person.EmailAddress.BusinessEntityID%0A%20%20%20%20%20%20%20%20FOR%20JSON%20PATH)%20AS%20Emails%0AFROM%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Person.Person%0AWHERE%20Person.Person.BusinessEntityID%20%3D%20274%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3ESince%20FOR%20JSON%20clause%20returns%20a%20single%20text%20field%20you%20can%20put%20it%20in%20any%20column%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EID%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EFirstName%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3ELastName%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EEmails%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E274%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EStephen%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EJiang%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E%5B%7B%22EmailAddress%22%3A%22stephen0%40adventure-works.com%22%7D%2C%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%22EmailAddress%22%3A%22stephen.jiang%40outlook.com%22%7D%5D%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20case%20we%20are%20returning%20hybrid%20result%20set%20-%20relational%20data%20from%20Person%20table%20are%20returned%20as%20columns%2C%20while%20related%20information%20from%20Person.emailAddress%20table%20are%20returned%20as%20an%20array%20of%20JSON%20objects.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20might%20notice%20that%20FOR%20JSON%20returns%20an%20array%20of%20key%3Avalue%20pairs%20even%20if%20we%20might%20like%20to%20have%20something%20simpler%20e.g.%20plain%20array%20of%20values%20instead%20of%20array%20of%20objects.%20In%20this%20case%20we%20can%20write%20simple%20T-SQL%20user%20defined%20function%20that%20removes%20keys%20from%20the%20array%20and%20return%20plain%20array%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Person.Person.BusinessEntityID%20AS%20ID%2C%20Person.Person.FirstName%2C%20Person.Person.LastName%2C%0Adbo.ufnToRawJsonArray((SELECT%20Person.EmailAddress.EmailAddress%0AFROM%20Person.EmailAddress%0AWHERE%20Person.Person.BusinessEntityID%20%3D%20Person.EmailAddress.BusinessEntityID%0AFOR%20JSON%20PATH)%2C%20'EmailAddress')%20AS%20Emails%0AFROM%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Person.Person%0AWHERE%20Person.Person.BusinessEntityID%20%3D%20274%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EResult%20of%20this%20query%20might%20look%20like%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EID%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EFirstName%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3ELastName%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EEmails%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E274%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EStephen%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EJiang%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E%5B%22stephen0%40adventure-works.com%22%2C%22stephen.jiang%40outlook.com%22%5D%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20more%20readable%20format%20that%20array%20of%20key%20value%20pairs.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--21754980%22%20id%3D%22toc-hId--21754980%22%3EUse%20Case%202%3A%20Formatting%20related%20information%20from%20multiple%20child%20tables%20as%20JSON%20arrays%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20previous%20use%20case%2C%20I%20have%20returned%20content%20of%20one%20related%20child%20table%20as%20JSON%20array.%20We%20can%20also%20return%20related%20information%20both%20from%20emails%20and%20phone%20tables.%20Second%20set%20of%20related%20information%20would%20be%20returned%20as%20column%20level%20JSON%20expression%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Person.Person.BusinessEntityID%20AS%20ID%2C%20Person.Person.FirstName%2C%20Person.Person.LastName%2C%0A%20%20%20%20%20%20(SELECT%20Person.EmailAddress.EmailAddress%0A%20%20%20%20%20%20%20%20%20FROM%20Person.EmailAddress%0A%20%20%20%20%20%20%20WHERE%20Person.Person.BusinessEntityID%20%3D%20Person.EmailAddress.BusinessEntityID%0A%20%20%20%20%20%20%20FOR%20JSON%20PATH)%20AS%20Emails%2C%0A%20%20%20%20%20%20%20(SELECT%20Person.PersonPhone.PhoneNumber%2C%20Person.PhoneNumberType.Name%20AS%20PhoneNumberType%0A%20%20%20%20%20%20%20%20%20%20FROM%26nbsp%3B%20Person.PersonPhone%20INNER%20JOIN%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Person.PhoneNumberType%20ON%20Person.PersonPhone.PhoneNumberTypeID%20%3D%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Person.PhoneNumberType.PhoneNumberTypeID%0A%20%20%20%20%20%20%20%20%20%20%20%20WHERE%20Person.Person.BusinessEntityID%20%3D%20Person.PersonPhone.BusinessEntityID%0A%20%20%20%20%20%20%20%20%20FOR%20JSON%20PATH)%20AS%20Phones%0AFROM%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Person.Person%0AWHERE%20Person.Person.BusinessEntityID%20%3D%20274%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20case%2C%20we%20have%20returned%20a%20single%20row%20for%20a%20particular%20person%20and%20all%20related%20information%20are%20returned%20as%20an%20array%20of%20JSON%20object%20in%20the%20single%20line.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EID%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EFirstName%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3ELastName%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EEmails%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EPhones%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E274%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EStephen%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3EJiang%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E%5B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%22EmailAddress%22%3A%22stephen0%40adventure-works.com%22%7D%2C%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%22EmailAddress%22%3A%22stephen.jiang%40outlook.com%22%7D%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%5D%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E%5B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%22PhoneNumber%22%3A%22112-555-6207%22%2C%22PhoneNumberType%22%3A%22Work%22%7D%2C%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%22PhoneNumber%22%3A%22238-555-0197%22%2C%22PhoneNumberType%22%3A%22Cell%22%7D%2C%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%22PhoneNumber%22%3A%22817-555-1797%22%2C%22PhoneNumberType%22%3A%22Home%22%7D%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%5D%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20easily%20unpack%20these%20arrays%20on%20the%20client%20side%20using%20some%20JSON%20deserializer%20such%20as%20JSON.NET.%20You%20might%20notice%20that%20we%20still%20have%20one%20array.%20The%20fact%20that%20we%20are%20adding%20new%20child%20items%20do%20not%20affects%20number%20of%20returned%20results.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1721055355%22%20id%3D%22toc-hId-1721055355%22%3EUse%20Case%203%3A%20Formatting%20parent%20and%20child%20rows%20as%20JSON%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EInstead%20of%20hybrid%20result%20set%2C%20we%20can%20format%20all%20results%20as%20JSON%20(i.e.%20both%20parent%20Person%20columns%20and%20child%20rows).%20If%20you%20want%20to%20return%20all%20results%20as%20JSON%20there%20is%20even%20simpler%20syntax.%20FOR%20JSON%20AUTO%20will%20automatically%20indent%20related%20email%20addresses%20as%20it%20is%20shown%20in%20the%20following%20script%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Person.Person.BusinessEntityID%2C%20Person.Person.FirstName%2C%20Person.Person.LastName%2C%20Person.EmailAddress.EmailAddress%0AFROM%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Person.Person%0A%20%20%20%20INNER%20JOIN%20Person.EmailAddress%0A%20%20%20%20%20%20%20%20%20%20ON%20Person.Person.BusinessEntityID%20%3D%20Person.EmailAddress.BusinessEntityID%0AWHERE%20Person.Person.BusinessEntityID%20%3D%20274%0AFOR%20JSON%20AUTO%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EResults%20might%20look%20like%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%5B%3C%2FP%3E%0A%3CP%3E%7B%20%22ID%22%3A274%2C%22FirstName%22%3A%22Stephen%22%2C%22LastName%22%3A%22Jiang%22%2C%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%22Person.EmailAddress%22%3A%5B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%22EmailAddress%22%3A%22stephen0%40adventure-works.com%22%7D%2C%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%22EmailAddress%22%3A%22stephen.jiang%40outlook.com%22%7D%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%5D%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7D%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%5D%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20format%20is%20pure%20JSON%20and%20it%20can%20be%20used%20as%20a%20response%20of%20JSON%20web%20service.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--831101606%22%20id%3D%22toc-hId--831101606%22%3EUse%20Case%204%3A%20Returning%20complex%20JSON%20hierarchies%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFinally%20we%20can%20format%20entire%20result-set%20as%20JSON%20by%20using%20FOR%20JSON%20cause%20on%20the%20main%20query%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Person.Person.BusinessEntityID%20AS%20ID%2C%20Person.Person.FirstName%2C%20Person.Person.LastName%2C%0A%20%20%20%20%20%20%20(SELECT%20Person.EmailAddress.EmailAddress%0A%20%20%20%20%20%20%20%20%20%20%20FROM%20Person.EmailAddress%0A%20%20%20%20%20%20%20%20%20WHERE%20Person.Person.BusinessEntityID%20%3D%20Person.EmailAddress.BusinessEntityID%0A%20%20%20%20%20%20%20%20%20FOR%20JSON%20PATH)%20AS%20Emails%2C%0A%20%20%20%20%20%20%20%20(SELECT%20Person.PersonPhone.PhoneNumber%2C%20Person.PhoneNumberType.Name%20AS%20PhoneNumberType%0A%20%20%20%20%20%20%20%20%20%20%20%20%20FROM%26nbsp%3B%20Person.PersonPhone%0A%20%20%20%20%20%20%20%20%20%20%20%20%20INNER%20JOIN%20Person.PhoneNumberType%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ON%20Person.PersonPhone.PhoneNumberTypeID%20%3D%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Person.PhoneNumberType.PhoneNumberTypeID%0A%20%20%20%20%20%20%20%20%20%20%20WHERE%20Person.Person.BusinessEntityID%20%3D%20Person.PersonPhone.BusinessEntityID%0A%20%20%20%20%20%20%20%20%20%20%20%20FOR%20JSON%20PATH)%20AS%20Phones%0AFROM%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Person.Person%0AWHERE%20Person.Person.BusinessEntityID%20%3D%20274%0AFOR%20JSON%20PATH%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EResults%20will%20look%20like%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CBR%20%2F%3E%3CP%3E%5B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%22ID%22%3A274%2C%22FirstName%22%3A%22Stephen%22%2C%22LastName%22%3A%22Jiang%22%2C%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%22Emails%22%3A%5B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%22EmailAddress%22%3A%22stephen0%40adventure-works.com%22%7D%2C%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%22EmailAddress%22%3A%22stephen.jiang%40outlook.com%22%7D%5D%2C%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%22Phones%22%3A%5B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%22PhoneNumber%22%3A%22112-555-6207%22%2C%22PhoneNumberType%22%3A%22Work%22%7D%2C%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%22PhoneNumber%22%3A%22238-555-0197%22%2C%22PhoneNumberType%22%3A%22Cell%22%7D%2C%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%22PhoneNumber%22%3A%22817-555-1797%22%2C%22PhoneNumberType%22%3A%22Home%22%7D%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%5D%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7D%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%5D%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFOR%20JSON%20clause%20enables%20you%20to%20format%20complex%20results%20and%20return%20them%20in%20more%20convenient%20format%20to%20the%20client.%20Note%20that%20you%20will%20need%20SQL%20Server%202016%20CTP2%20or%20higher%20for%20these%20code%20examples.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-384482%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20MSDN%20on%20Oct%2009%2C%202015%20In%20this%20post%20I%20will%20talk%20about%20one%20annoying%20thing%20%E2%80%93%20how%20to%20return%20a%20result%20set%20containing%20one%20to%20many%20relationships%20between%20tables.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-384482%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerStorageEngine%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Sep 29 2020 08:03 AM
Updated by: