Add a root node to JSON output

By specifying the ROOT option in the FOR JSON query, you can add a single, top-level element to the JSON output. The following code shows this:

SELECT TOP (3) PersonID, FullName, EmailAddress, PhoneNumber 
FROM Application.People ORDER BY PersonID ASC FOR JSON AUTO, ROOT('Persons'); 

Here is the result:

{ 
   "Persons":[ 
      { 
         "PersonID":1, 
         "FullName":"Data Conversion Only" 
      }, 
      { 
         "PersonID":2, 
         "FullName":"Kayla Woodcock", 
         "EmailAddress":"[email protected]", 
         "PhoneNumber":"(415) 555-0102" 
      }, 
      { 
         "PersonID":3, 
         "FullName":"Hudson Onslow", 
         "EmailAddress":"[email protected]", 
         "PhoneNumber":"(415) 555-0102" 
      } 
   ] 
} 

By specifying the root element, you have converted the outer array to a single complex property named Persons.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.74.3