Click here to monitor SSC
  • Av rating:
  • Total votes: 55
  • Total comments: 6
Robert Sheldon

JSON support in SQL Server 2016

15 December 2015

At last, SQL Server has caught up with other RDBMSs by providing a useful measure of JSON-support. It is a useful start, even though it is nothing like as comprehensive as the existing XML support. For many applications, what is provided will be sufficient. Robert Sheldon describes what is there and what isn't.

SQL Server 2016 is finally adding support for JSON, a lightweight format for exchanging data between different source types, similar to how XML is used. JSON, short for JavaScript Object Notation, is based on a subset of the JavaScript programming language and is noted for being human readable and easy for computers to parse and generate.

According to Microsoft, it is one of the most highly ranked requests on the Microsoft connect site and so for many, its inclusion in SQL Server is welcome news. That is, unless you were expecting the same sort of robust support we've seen with XML. SQL Server 2016 does not approach JSON with such vehemence, nor does it match what you'll find in products such as PostgreSQL.

SQL Server 2016 includes no JSON-specific data type and consequently none of the kinds of methods available to the XML data type. SQL Server 2016 continues to use the NVARCHAR type to store JSON data. However, it does provide several important T-SQL language elements that make working with JSON much easier than it has been in the past, so Microsoft is at least moving in the right direction, even if it still has some catching up to do.

Getting to know JSON

Although JSON is a bit more complex than what we'll cover here, it can help to have a basic understanding of what makes up a JSON code snippet before starting in on the SQL Server support. At its most basic, a JSON snippet can contain objects, arrays, or both. An object is an unordered collection of one or more name/value pairs (properties), enclosed in curly braces, as shown in the following example:

	{"FirstName":"Terri", "Current":true, "Age":42, "Phone":null}

For each property, the name component (FirstName, Current, Age, and Phone) is enclosed in double quotes and followed by a colon. The name component, sometimes referred to as the key, is always a string. The property's value follows slightly different rules. If the value is a string, you should enclose it in double quotes. If it is a numeric value, Boolean value (true or false), or null value, do not enclose it in quotes.

An array is simply an ordered collection of values, enclosed in square brackets, as in the following example:

	["Terri", true, 42, null]

An array supports the same types of values as an object: string, number, true, false, or null. In addition, both objects and arrays can contain other objects and arrays as their values, providing a way to nest structures, as shown in the following example:

{
   "Employees":[
      {
         "Name":{
            "First":"Terri",
            "Middle":"Lee",
            "Last":"Duffy"
         },
         "PII":{
            "DOB":"1971-08-01",
            "NatID":"245797967"
         },
         "LoginID":"adventure-works\\terri0"
      },
      {
         "Name":{
            "First":"Roberto",
            "Middle":null,
            "Last":"Tamburello"
         },
         "PII":{
            "DOB":"1974-11-12",
            "NatID":"509647174"
         },
         "LoginID":"adventure-works\\roberto0"
      }
   ]
}

At the top level, we have a JSON object that includes a single property. The property's name is Employees, and the value is an array, which contains two values. Each array value is a JSON object that includes the Name, PII, and LoginID properties. The Name and PII values are also JSON objects, which contain their own name/value pairs.

As we work through the examples in this article, you'll get a better sense of how these various components work.

Formatting query results as JSON

One of the JSON-related features supported in SQL Server 2016 is the ability to return data in the JSON format, which we do by adding the FOR JSON clause to a SELECT statement. We'll explore the basics of how to use a FOR JSON clause to return data in the JSON format, using either the AUTO argument or the PATH argument.

First, however, we need some data on which to work. The following SELECT statement retrieves two rows from the vEmployee view in the AdventureWorks2016CTP3 database:

USE AdventureWorks2016CTP3;
go

SELECT FirstName, MiddleName, LastName, 
  EmailAddress, PhoneNumber
FROM HumanResources.vEmployee
WHERE BusinessEntityID in (2, 3);

It returns the following results, although you might see some differences with the final product, since the data and examples are based on the CTP 3 release of SQL Server 2016:

FirstNameMiddleName LastNameEmailAddress PhoneNumber
TerriLeeDuffyterri0@adventure-works.com819-555-0175
RobertoNULLTamburelloroberto0@adventure-works.com212-555-0187

AUTO mode

To return these results as JSON, to support a specific application, we simply add the FOR JSON clause to the statement, as shown in the following example.

SELECT FirstName, MiddleName, LastName, 
  EmailAddress, PhoneNumber
FROM HumanResources.vEmployee
WHERE BusinessEntityID in (2, 3)
FOR JSON AUTO;

Notice that the clause includes the AUTO argument, which indicates that the results should be returned in AUTO mode. When you specify this mode, the database engine automatically determines the JSON format, based on the order of the columns in the SELECT list and the tables in the FROM clause. In this case, the FOR JSON AUTO clause causes the SELECT statement to return the following results.

	[{"FirstName":"Terri","MiddleName":"Lee","LastName":"Duffy","EmailAddress":"terri0@adventure-works.com","PhoneNumber":"819-555-0175"},{"FirstName":"Roberto","LastName":"Tamburello","EmailAddress":"roberto0@adventure-works.com","PhoneNumber":"212-555-0187"}]

From these results, you might be able to see that the JSON output includes an array that contains two values, with each value a JSON object. Not surprisingly, as the results become more involved, it becomes more difficult to read them. In such cases, you can use a local or online JSON formatter/validator to turn the JSON snippet into something more readable. For example, I fed the previous results into the formatter at https://jsonformatter.curiousconcept.com/ and came up with the following JSON:

[
   {
      "FirstName":"Terri",
      "MiddleName":"Lee",
      "LastName":"Duffy",
      "EmailAddress":"terri0@adventure-works.com",
      "PhoneNumber":"819-555-0175"
   },
   {
      "FirstName":"Roberto",
      "LastName":"Tamburello",
      "EmailAddress":"roberto0@adventure-works.com",
      "PhoneNumber":"212-555-0187"
   }
]

As you can see, it is now much easier to see our top-level array and the two object values it contains. Each object corresponds to a row returned by the SELECT statement. Going forward, I'll show only the formatter-fed results so they're more readable, but know that SQL Server returns the data as a single-line value, without all the whitespace and line breaks, as you saw above.

Now that you've gotten a taste of the FOR JSON AUTO clause, let's look at what happens when we join tables:

SELECT e.BirthDate, e.NationalIDNumber, e.LoginID,
  p.FirstName, p.MiddleName, p.LastName
FROM HumanResources.Employee e INNER JOIN Person.Person p
  ON e.BusinessEntityID = p.BusinessEntityID
WHERE e.BusinessEntityID in (2, 3)
FOR JSON AUTO;

As our SELECT statement becomes more complex, so too does the JSON output, as shown in the following results:

[ 
   { 
      "BirthDate":"1971-08-01",
      "NationalIDNumber":"245797967",
      "LoginID":"adventure-works\\terri0",
      "p":[
         {
            "FirstName":"Terri",
            "MiddleName":"Lee",
            "LastName":"Duffy"
         }
      ]
   },
   {
      "BirthDate":"1974-11-12",
      "NationalIDNumber":"509647174",
      "LoginID":"adventure-works\\roberto0",
      "p":[
         {
            "FirstName":"Roberto",
            "LastName":"Tamburello"
         }
      ]
   }
]

The information from the Person table is now part of the p array, which itself is one of the values in the parent object. As you'll recall, AUTO mode formats the results based on the order of the columns in the SELECT list and the tables in the FROM clause, so let's mix up that column order:

SELECT p.FirstName, p.MiddleName, p.LastName,
  e.BirthDate, e.NationalIDNumber, e.LoginID
FROM HumanResources.Employee e INNER JOIN Person.Person p
  ON e.BusinessEntityID = p.BusinessEntityID
WHERE e.BusinessEntityID in (2, 3)
FOR JSON AUTO;

Now the SELECT statement will return the JSON with the data from the Employee table treated as the nested object:

[
   {
      "FirstName":"Terri",
      "MiddleName":"Lee",
      "LastName":"Duffy",
      "e":[
         {
            "BirthDate":"1971-08-01",
            "NationalIDNumber":"245797967",
            "LoginID":"adventure-works\\terri0"
         }
      ]
   },
   {
      "FirstName":"Roberto",
      "LastName":"Tamburello",
      "e":[
         {
            "BirthDate":"1974-11-12",
            "NationalIDNumber":"509647174",
            "LoginID":"adventure-works\\roberto0"
         }
      ]
   }
]

As you can see, we have two e arrays, embedded in the outer objects. We can continue to play around with our SELECT statement to try to get closer to the JSON results we want, or we can instead use the PATH mode, which gives us full control over the format of the JSON output. For all but the most basic SELECT statements, you'll likely want to use the PATH mode.

PATH mode

To use the PATH mode, we start be specifying PATH in the FOR JSON clause, rather than AUTO, as shown in the following example:

SELECT p.FirstName, p.MiddleName, p.LastName,
  e.BirthDate, e.NationalIDNumber, e.LoginID
FROM HumanResources.Employee e INNER JOIN Person.Person p
  ON e.BusinessEntityID = p.BusinessEntityID
WHERE e.BusinessEntityID in (2, 3)
FOR JSON PATH;

When we switch to the PATH mode, the database engine flattens out our results and returns the data as two object values within a single array:

[
   {
      "FirstName":"Terri",
      "MiddleName":"Lee",
      "LastName":"Duffy",
      "BirthDate":"1971-08-01",
      "NationalIDNumber":"245797967",
      "LoginID":"adventure-works\\terri0"
   },
   {
      "FirstName":"Roberto",
      "LastName":"Tamburello",
      "BirthDate":"1974-11-12",
      "NationalIDNumber":"509647174",
      "LoginID":"adventure-works\\roberto0"
   }
]

Using the PATH mode in this way is fairly straightforward; however, this is PATH at its most basic. The mode lets us be far more specific. For example, we can control how the the database engine nests the JSON output by specifying column aliases that define the structure, as shown in the following SELECT clause:

SELECT
  p.FirstName AS [Name.First],
  p.MiddleName AS [Name.Middle],
  p.LastName AS [Name.Last],
  e.BirthDate AS [PII.DOB], 
  e.NationalIDNumber AS [PII.NatID], 
  e.LoginID
FROM HumanResources.Employee e INNER JOIN Person.Person p
  ON e.BusinessEntityID = p.BusinessEntityID
WHERE e.BusinessEntityID in (2, 3)
FOR JSON PATH;

In this case, we are defining the Name object, which contains the First, Middle, and Last values; the PII object, which contains the DOB and NatID values; and the LoginID name/value pair, as shown in the following results:

[
   {
      "Name":{
         "First":"Terri",
         "Middle":"Lee",
         "Last":"Duffy"
      },
      "PII":{
         "DOB":"1971-08-01",
         "NatID":"245797967"
      },
      "LoginID":"adventure-works\\terri0"
   },
   {
      "Name":{
         "First":"Roberto",
         "Last":"Tamburello"
      },
      "PII":{
         "DOB":"1974-11-12",
         "NatID":"509647174"
      },
      "LoginID":"adventure-works\\roberto0"
   }
]

In some cases, you will want to add a single, top-level element to your JSON output to serve as a root. To do so, you must specify it as part of the FOR JSON clause, as shown in the following example:

SELECT
  p.FirstName AS [Name.First],
  p.MiddleName AS [Name.Middle],
  p.LastName AS [Name.Last],
  e.BirthDate AS [PII.DOB], 
  e.NationalIDNumber AS [PII.NatID], 
  e.LoginID
FROM HumanResources.Employee e INNER JOIN Person.Person p
  ON e.BusinessEntityID = p.BusinessEntityID
WHERE e.BusinessEntityID in (2, 3)
FOR JSON PATH, ROOT('Employees');

To specify the root, we add the ROOT option to the FOR JSON clause and, in this case, name the root Employees, which gives us the following results:

{
   "Employees":[
      {
         "Name":{
            "First":"Terri",
            "Middle":"Lee",
            "Last":"Duffy"
         },
         "PII":{
            "DOB":"1971-08-01",
            "NatID":"245797967"
         },
         "LoginID":"adventure-works\\terri0"
      },
      {
         "Name":{
            "First":"Roberto",
            "Last":"Tamburello"
         },
         "PII":{
            "DOB":"1974-11-12",
            "NatID":"509647174"
         },
         "LoginID":"adventure-works\\roberto0"
      }
   ]
}

If you compare these results to those from the previous example, you will see that the outer element has been changed from an array to an object that contains only the Employees property. The Employees value is now the array that was the outer element in the previous example.

You might have also noticed that the second employee, Roberto, includes no middle name. That is because the MiddleName column in the source table is null. By default, the database engine does not include a JSON element whose value is null. However, you can override this behavior by adding the INCLUDE_NULL_VALUES option to the FOR JSON clause, as shown in the following SELECT statement:

SELECT

SELECT
  p.FirstName AS [Name.First],
  p.MiddleName AS [Name.Middle],
  p.LastName AS [Name.Last],
  e.BirthDate AS [PII.DOB], 
  e.NationalIDNumber AS [PII.NatID], 
  e.LoginID
FROM HumanResources.Employee e INNER JOIN Person.Person p
  ON e.BusinessEntityID = p.BusinessEntityID
WHERE e.BusinessEntityID in (2, 3)
FOR JSON PATH, ROOT('Employees'), INCLUDE_NULL_VALUES;

Now the results will show that Roberto's middle name is null by assigning the null value to the Middle property:

{
   "Employees":[
      {
         "Name":{
            "First":"Terri",
            "Middle":"Lee",
            "Last":"Duffy"
         },
         "PII":{
            "DOB":"1971-08-01",
            "NatID":"245797967"
         },
         "LoginID":"adventure-works\\terri0"
      },
      {
         "Name":{
            "First":"Roberto",
            "Middle":null,
            "Last":"Tamburello"
         },
         "PII":{
            "DOB":"1974-11-12",
            "NatID":"509647174"
         },
         "LoginID":"adventure-works\\roberto0"
      }
   ]
}

There are, of course, other considerations to take into account when using this clause, so be sure to refer to SQL Server 2016 documentation. In the meantime, let's look at how to convert a JSON snippet to traditional rowset data.

Converting JSON to rowset data using the OPENJSON function

To return a JSON snippet as rowset data, we use the OPENJSON rowset function to convert the data to a relational format. The function returns three values:

  • key: Property name within the object or index of the element within the array.
  • value: Property value within the object or value of the array element specified by the index.
  • type: Value's data type, represented numerically, as described in the following table:
Numeric valueData type
0null
1string
2int
3true or false
4array
5object

To test how the the OPENJSON function works, let's assign a JSON snippet to a variable and then use the function to call the variable, as shown in the following example:

DECLARE @json NVARCHAR(MAX) = N'
{
  "FirstName":null,
  "LastName":"Duffy",
  "NatID":245797967,
  "Current":false,
  "Skills":["Dev","QA","PM"],
  "Region":{"Country":"Canada","Territory":"North America"}
}';

SELECT * FROM OPENJSON(@json);

The JSON snippet contains a single object that includes a property for each data type. The SELECT statement uses the OPENJSON rowset function within the FROM clause to retrieve the JSON data as a rowset, as shown in the following results:

keyvaluetype
FirstNameNULL0
LastNameDuffy1
NatID2457979672
Currentfalse3
Skills["Dev","QA","PM"]4
Region{"Country":"Canada","Territory":"North America"}5

Notice that the type column in the results identifies the data type for each value. As expected, the column shows the Skills value an array, with all of the array's elements included in the results for that row. The same goes for the Region value, which is an object. The row includes all the properties within that object.

In some cases, you will want to return only the key and value columns, so you will need to specify those columns in your SELECT list:

SELECT [key], value
FROM OPENJSON(@json);

Notice that you must delimit the key column because Microsoft chose to return a column name that is also a T-SQL reserved keyword. As the following table shows, the results include only those two columns:

keyvalue
FirstNameNULL
LastNameDuffy
NatID245797967
Currentfalse
Skills["Dev","QA","PM"]
Region{"Country":"Canada","Territory":"North America"}

Now let's move on to a more complex JSON snippet, which we'll use for the remaining examples in this article:

{
   "Employees":[
      {
         "Name":{
            "First":"Terri",
            "Middle":"Lee",
            "Last":"Duffy"
         },
         "PII":{
            "DOB":"1971-08-01",
            "NatID":"245797967"
         },
         "LoginID":"adventure-works\\terri0"
      },
      {
         "Name":{
            "First":"Roberto",
            "Middle":null,
            "Last":"Tamburello"
         },
         "PII":{
            "DOB":"1974-11-12",
            "NatID":"509647174"
         },
         "LoginID":"adventure-works\\roberto0"
      }
   ]
}

The JSON shown here comes from the output generated from the last example in the preceding section. As you'll recall, the database engine actually outputs the JSON in a format much less readable than what is shown here, but it can be easier to work with when assigning the JSON to a variable. So that's the approach we'll take for the remaining examples:

DECLARE @json NVARCHAR(MAX) = N'{"Employees":[{"Name":{"First":"Terri","Middle":"Lee","Last":"Duffy"},"PII":{"DOB":"1971-08-01","NatID":"245797967"},"LoginID":"adventure-works\\terri0"},{"Name":{"First":"Roberto","Middle":null,"Last":"Tamburello"},"PII":{"DOB":"1974-11-12","NatID":"509647174"},"LoginID":"adventure-works\\roberto0"}]}';

If you plan to try out the next batch of examples, you can use this variable definition for each one, which avoids all the whitespace you get when you run the results through a parser. Now let's use the OPENJSON function to convert the JSON in the variable:

SELECT [key], value
FROM OPENJSON(@json);

The example uses OPENJSON at its most basic, with no other parameters defined. As a result, the SELECT statement returns only a single row for the Employees array, as shown in the following table:

keyvalue
Employees[{"Name":{"First":"Terri","Middle":"Lee","Last":"Duffy"},"PII":{"DOB":"1971-08-01","NatID":"245797967"},"LoginID":"adventure-works\\terri0"},{"Name":{"First":"Roberto","Middle":null,"Last":"Tamburello"},"PII":{"DOB":"1974-11-12","NatID":"509647174"},"LoginID":"adventure-works\\roberto0"}]

To better control our results, we need to pass a second argument into the OPENJSON function. The argument is a JSON path that instructs the database engine on how to parse the data. For example, the following path instructs the database engine to return data based on the Employees property:

SELECT [key], value
FROM OPENJSON(@json, '$.Employees');

When you specify a JSON path, you start with a dollar sign ($) to represent the item as it exists in its current context. You then specify one or more elements as they appear hierarchically in the JSON snippet, using periods to separate the elements. In this case, the path specifies only the root element, Employees, giving us the results shown in the following table:

keyvalue
0{"Name":{"First":"Terri","Middle":"Lee","Last":"Duffy"},"PII":{"DOB":"1971-08-01","NatID":"245797967"},"LoginID":"adventure-works\\terri0"}
1{"Name":{"First":"Roberto","Middle":null,"Last":"Tamburello"},"PII":{"DOB":"1974-11-12","NatID":"509647174"},"LoginID":"adventure-works\\roberto0"}

This time, we get a row for each element in the Employees array. If we want to break the results down even further, we must work down the hierarchy. For example, to reference an element within the Employees array, we must specify the element's index, as it exists within the array. An array's index is zero-based, which means the index count starts with 0, so if we want to retrieve the first element in the Employees array, we must specify 0 after the root name, within square brackets, as shown in the following statement:

SELECT [key], value
FROM OPENJSON(@json, '$.Employees[0]');

The first element in the Employees array is a JSON object that contains three properties, so that is what the SELECT statement returns, as shown in the following results:

keyvalue
Name{"First":"Terri","Middle":"Lee","Last":"Duffy"}
PII{"DOB":"1971-08-01","NatID":"245797967"}
LoginIDadventure-works\terri0

Because the first two values are objects, the entire contents of those objects are returned. However, we can instead return only one of those objects by specify the object name:

SELECT [key], value
FROM OPENJSON(@json, '$.Employees[0].Name');

Now the SELECT statement returns only the three properties within the Name object:

keyvalue
FirstTerri
MiddleLee
LastDuffy

The OPENJSON examples we've looked at so far have used the default schema when returning the data as a rowset, but there are limits to how well we can control the results. Fortunately, the OPENJSON function also lets us add a WITH clause to our SELECT statement in order to define an explicit schema. In the following example, the schema flattens out our data so we can easily see the details for each employee:

SELECT *
FROM OPENJSON(@json, '$.Employees')
WITH([Name.First] NVARCHAR(25), [Name.Middle] NVARCHAR(25), 
  [Name.Last] NVARCHAR(25), [PII.DOB] DATE, [PII.NatID] INT);

The WITH clause specifies each column, using names that link to the original JSON. For example, the Name.First column returns the employee's first name. The column name is based on the First property within the Name object. For each column, we also provide a T-SQL data type. The SELECT statement now returns the results shown in the following table:

Name.FirstName.MiddleName.LastPII.DOBPII.NatID
TerriLeeDuffy1971-08-01245797967
RobertoNULLTamburello1974-11-12509647174

If we want to define more readable column names, we can instead create column definitions that each includes the new name, followed the data type, and then a path reference, as shown in the following example:

SELECT *
FROM OPENJSON(@json, '$.Employees')
WITH(FirstName NVARCHAR(25) '$.Name.First', 
  MiddleName NVARCHAR(25) '$.Name.Middle', 
  LastName NVARCHAR(25) '$.Name.Last', 
  BirthDate DATE '$.PII.DOB', 
  NationalID INT '$.PII.NatID');

Notice that, for the path, we do not need to reference the Employees array itself. That's taken care of in the OPENJSON function. But we still need to specify the dollar sign to show the current context. We then follow with the Name or PII object name and then the property name. The SELECT statement now returns the results shown in the following table:

FirstNameMiddleNameLastNameBirthDateNationalID
TerriLeeDuffy1971-08-01245797967
RobertoNULLTamburello1974-11-12509647174

The preceding examples should give you at least a basic idea of how to turn a JSON snippet into rowset data. Again, refer to SQL Server 2016 documentation to get more specifics about how to use the OPENJSON function.

More JSON functions in SQL Server 2016

In addition to OPENJSON, SQL Server 2016 includes several other functions for working with JSON data. We'll review how to use the ISJSON, JSON_value functions, and JSON_ QUERY functions.

ISJSON

The ISJSON function lets you test whether a text string is correctly formatted JSON. This is a particularly important function, considering that SQL Server 2016 doesn't support a JSON data type. At least this way, you have some way to validate your data.

The ISJSON function returns 1 if a string is valid JSON, otherwise returns 0. The only exception to this is if the string is null, in which case the function returns null. The following SELECT statement tests our ubiquitous @json variable to verify whether it is valid:

SELECT CASE 
  WHEN ISJSON(@json) > 0 
    THEN 'The variable value is JSON.' 
    ELSE 'The variable value is not JSON.' 
  END;

As we hoped, the SELECT statement returns the following results:

The variable value is JSON.

Now let's pass in text that is not valid JSON by tagging on the Age element without a value:

DECLARE @json2 NVARCHAR(MAX) = N'
{"First":"Terri","Middle":"Lee","Last":"Duffy","Age"}';

SELECT CASE 
  WHEN ISJSON(@json2) > 0 
    THEN 'The variable value is JSON.' 
    ELSE 'The variable value is not JSON.' 
  END;

As expected, we receive the second message:

The variable value is not JSON.

JSON_VALUE

Another handy JSON-related function in SQL Server 2016 is JSON_VALUE, which lets us extract a scalar value from a JSON snippet, as shown in the following example:

SELECT JSON_VALUE(@json, '$.Employees[0].Name.First');

The JSON_VALUE function takes two arguments. The first is the JSON itself, and the second is a path that defines which element's value we want to retrieve. In this case, the path specifies the First property in the Name object, which is part of the first element in the Employees array. As we would expect, the SELECT statement returns the value Terri.

We can just as easily return the NatID value for the second employee:

SELECT JSON_VALUE(@json, '$.Employees[1].PII.NatID');

Now the SELECT statement returns 509647174. Suppose, however, that we try to retrieve something other than a scalar value. For example, the following path specifies only the PII object for the second employee:

SELECT JSON_VALUE(@json, '$.Employees[1].PII');

This time, the SELECT statement returns a null value. By default, the database engine returns a null value if the path does not exist or is not applicable to the current situation. In this example, we've specified an element that cannot return a scalar value, so the database engine returns the null value.

When specifying a path in a JSON-related expression, you can control the results by preceding the path with the lax or strict option. The lax option is the default and is implied if not specified, which means that the database engine returns a null value if a problem arises. For example, the following path explicitly includes the lax option:

SELECT JSON_VALUE(@json, 'lax $.Employees[1].PII');

Once again, out statement returns a null value because we're specifying an element that cannot return a scalar value. We can instead specify the strict option, in which case, the database engine will raise an error if a problem occurs:

SELECT JSON_VALUE(@json, 'strict $.Employees[1].PII');

This time we receive very different results:

Property cannot be found in specified path.

JSON_QUERY

Another useful JSON-related tool is the JSON_QUERY function, which can extract an object or array from a JSON snippet. For example, the following SELECT statement retrieves the PII object for the second employee:

SELECT JSON_QUERY(@json, 'strict $.Employees[1].PII');

Like the JSON_value function, the JSON_QUERY function takes two arguments: the JSON source and a path indicating what data to extract. The SELECT statement returns the following results:

{"DOB":"1974-11-12","NatID":"509647174"}

If we want to return the Employees array, we simply specify $.Employees as our path:

SELECT JSON_QUERY(@json, 'strict $.Employees');

Now the SELECT statement returns just about everything in our JSON snippet:

[{"Name":{"First":"Terri","Middle":"Lee","Last":"Duffy"},"PII":{"DOB":"1971-08-01","NatID":"245797967"},"LoginID":"adventure-works\\terri0"},{"Name":{"First":"Roberto","Middle":null,"Last":"Tamburello"},"PII":{"DOB":"1974-11-12","NatID":"509647174"},"LoginID":"adventure-works\\roberto0"}]

Summary: JSON and SQL Server 2016

This article should give you what you need to start working with JSON data in SQL Server. As you can see, however, JSON support is nowhere nearly as robust as XML support. And if you're working with other database management systems, you'll quickly discover that the JSON features in SQL Server 2016 have some catching up to do before they can match what's been implemented in other products.

Even so, what SQL Server 2016 provides is better than nothing, and the JSON support is solid and could prove more than adequate much of the time. In fact, for some organizations, the JSON features already implemented in SQL Server 2016 will be enough to meet their needs. Best of all, the JSON-related functionality is straightforward and easy-to-use, so you should be able to incorporate it into your workflow with relatively little pain.

Robert Sheldon

Author profile:

After being dropped 35 feet from a helicopter and spending the next year recovering, Robert Sheldon left the Colorado Rockies and emergency rescue work to pursue safer and less painful interests—thus his entry into the world of technology. He is now a technical consultant and the author of numerous books, articles, and training material related to Microsoft Windows, various relational database management systems, and business intelligence design and implementation. He has also written news stories, feature articles, restaurant reviews, legal summaries, and the novels 'Last Stand' and 'Dancing the River Lightly'. You can find more information at http://www.rhsheldon.com.

Search for other articles by Robert Sheldon

Rate this article:   Avg rating: from a total of 55 votes.


Poor

OK

Good

Great

Must read
Have Your Say
Do you have an opinion on this article? Then add your comment below:
You must be logged in to post to this forum

Click here to log in.


Subject: Performance
Posted by: Anonymous (not signed in)
Posted on: Wednesday, December 16, 2015 at 12:48 PM
Message: How is the performance of these JSON functions in SQL Server 2016? How does it perform if you have a big JSON blob and are going against thousands/millions of rows of data?

Subject: Nice!
Posted by: Ray Starkey, Coventry, England (not signed in)
Posted on: Monday, December 21, 2015 at 1:11 AM
Message: Good article - powerful stuff. Nadela's on the right track.

Subject: Is JSON in SQL2016 merely lip service.
Posted by: Dave.Poole (view profile)
Posted on: Monday, December 21, 2015 at 4:54 AM
Message: It's great that SQL2016 has the ability to process and store JSON but I have this unworthy thought that it may be a quick hack piggy backing on existing XML methods.

I will be interested to see how Microsoft develop this feature further.

Subject: Interesting Info
Posted by: HaroldDev (view profile)
Posted on: Monday, June 6, 2016 at 2:34 PM
Message: I would like to recommend you to visit www.webcreek.com , where you’ll find awesome blog articles and very interesting information related to this topic.

Subject: NULL values returned
Posted by: Simon_L (view profile)
Posted on: Thursday, June 30, 2016 at 9:57 AM
Message: When I run the following code it returns NULL values .... any words of wisdom as to what Im doing wrong ?!


declare @json NVARCHAR (MAX) = N'
{
"Employees": [
{
"Name": {
"First": "Terri",
"Middle": "Lee",
"Last": "Duffy"
},
"PII": {
"DOB": "1971-08-01",
"NatId": "245797967"
},
"LoginID": "adventure-works\\terri0"
},
{
"Name": {
"First": "Roberto",
"Middle": null,
"Last": "Tamburello"
},
"PII": {
"DOB": "1974-11-12",
"NatId": "509647174"
},
"LoginID": "adventure-works\\roberto0"
}
]
}';



select * from openjson(@json,'$.Employees')
WITH (
[Name.First] nvarchar(25) ,
[Name.Middle] nvarchar(25) ,
[Name.Last] nvarchar(25) ,
[PII.dob] int,
[PII.Natid] date
);


Subject: Fix For Simon's JSON problem.
Posted by: ShadowDancerLV (view profile)
Posted on: Tuesday, July 12, 2016 at 3:34 PM
Message: @Simon
You need to add the '$.Fieldname' after each of your field declared in your WITH clause.

For example:
[Name.Middle] nvarchar(25) '$.Name.Middle'

 
Simple-Talk Database Delivery

DLM
Patterns & Practices Library

Visit our patterns and practices library to learn more about database lifecycle management.

Find out how to automate the process of building, testing and deploying your database changes to reduce risk and make rapid releases possible.

Get started

Phil Factor
How to Build and Deploy a Database from Object-Level Source in a VCS

It is easy for someone who is developing a database to shrug and say 'if only my budget would extend to buying fancy... Read more...

 View the blog

Top Rated

Clone, Sweet Clone: Database Provisioning Made Easy?
 One of the difficulties of designing a completely different type of development tool such as SQL Clone... Read more...

Database Lifecycle Management: Deployment and Release
 So often, the unexpected delays in delivering database code are more likely to happen after the... Read more...

The PoSh DBA: Assigning Data to Variables Via PowerShell Common Parameters
 Sometimes, it is the small improvements in a language that can make a real difference. PowerShell is... Read more...

Issue Tracking for Databases
 Any database development project will be hard to manage without a system for reporting bugs in the... Read more...

Releasing Databases in VSTS with Redgate SQL CI and Octopus Deploy
 You can still do Database Lifecycle Management (DLM) workflows in the hosted version of Team foundation... Read more...

Most Viewed

Beginning SQL Server 2005 Reporting Services Part 1
 Steve Joubert begins an in-depth tour of SQL Server 2005 Reporting Services with a step-by-step guide... Read more...

Ten Common Database Design Mistakes
 If database design is done right, then the development, deployment and subsequent performance in... Read more...

Temporary Tables in SQL Server
 Temporary tables are used by every DB developer, but they're not likely to be too adventurous with... Read more...

Concatenating Row Values in Transact-SQL
 It is an interesting problem in Transact SQL, for which there are a number of solutions and... Read more...

SQL Server Index Basics
 Given the fundamental importance of indexes in databases, it always comes as a surprise how often the... Read more...

Why Join

Over 400,000 Microsoft professionals subscribe to the Simple-Talk technical journal. Join today, it's fast, simple, free and secure.