BT

SQL Server 2016: JSON Support

| by Jonathan Allen Follow 576 Followers on May 28, 2015. Estimated reading time: 4 minutes |

SQL Server 2016 is expected to offer native support for working with JSON. The first iteration of this support will be released as part of SQL Server 2016’s CTP 2. Also known as a “Community Tech Preview”, a CTP is Microsoft’s equivalent to a public alpha test wherein developers are allowed to recommend changes to the technology.

Exporting JSON

The main CTP 2 feature is the ability to “format and export” JSON. This is done by appending a FOR JSON clause to the end of a SELECT statement. It is based on the FOR XML clause and, like it, allows for both automatic and semi-manual formatting of the resulting JSON string.

Microsoft expects this syntax to be equivalent to PostgreSQL’s row_to_json and json_object functions.

Transforming JSON

The bulk of the feature set will be released in CTP 3. The first of these features is the FROM OPENJSON clause, which is a Table-Value Function (TFV) that accepts a JSON string as its input. It also requires a path to an array or object in the JSON data to be parsed.

By default, OPENJSON comes back as a set of key-value pairs, but you can supply a schema using a WITH clause. Since JSON doesn’t support dates or integers (they are represented as strings and doubles respectively), the WITH clause can reduce the amount of type conversions needed later.

Here is an example from Jovan Popovic’s article on JSON support.

DECLARE @JSalestOrderDetails nVarCar(2000) = N '{"OrdersArray": [
{"Number":1, "Date": "8/10/2012", "Customer": "Adventure works", "Quantity": 1200},
{"Number":4, "Date": "5/11/2012", "Customer": "Adventure works", "Quantity": 100},
{"Number":6, "Date": "1/3/2012", "Customer": "Adventure works", "Quantity": 250},
{"Number":8, "Date": "12/7/2012", "Customer": "Adventure works", "Quantity": 2200}
]}';

SELECT Number, Customer, Date, Quantity
FROM OPENJSON (@JSalestOrderDetails, '$.OrdersArray')
WITH (
    Number varchar(200),
    Date datetime,
    Customer varchar(200),
    Quantity int
) AS OrdersArray

To do the same in PostgreSQL, Microsoft claims that you need to use a combination of then json_each, json_object_keys, json_populate_record, and json_populate_recordset functions.

Storing JSON

As you can see, JSON data is stored as an nVarChar variable. This is also the case when storing JSON data inside table columns. Microsoft has several reasons for this:

  • Migration - We found that people already store JSON as text, so they would need to change database schema and reload data to use our new features if we introduce a separate JSON type. In our implementation, developers can use JSON functionalities without any change in database.
  • Cross feature compatibility – NVARCHAR is supported in all SQL Server components, so JSON will also be supported everywhere. You can put JSON in Hekaton, temporal or column store tables, apply standard security policies including row level security, use standard B-Tree and FTS indexes, use JSON as a parameter or return value of procedures and UDFs, etc. You don’t need to think does JSON works with feature X – if NVARCHAR works with feature X then JSON will also work. Again there are some constraints – Hekaton and column store do not support LOB values so you can put only small JSON documents there. However, once we add LOB support in Hekaton and column store you will be able to store large JSON documents everywhere.
  • Client-side support – Currently we don’t have standardized JSON object type in client apps (something like XmlDom object). Web and mobile application, and JavaScript clients will naturally use JSON text and parse it with their native parsers. In JavaScript, a type that represents JSON is object. It is unlikely that they will implement some proxy of JSON type that exists only in a few RDBMS. In C#.Net, a lot of developers use JSON.Net parser with built in JObject or JArray type; however it is not a standard and probably it will not be part of ADO.NET. Even in that case we believe that C# apps will accept plain strings from database layer and parse it with their favorite parser. We are not talking only about apps. If you try to use JSON column in SSIS/SSRS, Tableau, Informatica ETL, they will still see it as text. We believe that even if we add a JSON type it will be represented as string outside SQL Server and parsed with some custom parser if needed. Therefore, we have not found any major reason to implement it as native JSON type.

It is probably a good idea to use the new ISJSON function as a check constraint on your JSON-containing nVarChar columns. If you don’t, you risk data corruption from flawed client applications inserting unparsable strings.

Querying JSON

To directly query JSON for scalar values, you can use the JSON_VALUE function. This function uses a JavaScript like notation to locate values within the JSON object. It uses the $ symbol as the object root, dot-notation for properties, and brackets for array indexes. The PostgreSQL equivalent is json_extract_path_text.

JSON Indexes

JSON data cannot be directly indexed, but you can get the same effect for scalar data with minimal effort. Simply create a computed column using the JSON_VALUE function, then place an index on that.

“Missing” Features

Microsoft’s plan for JSON doesn’t include everything developers want. Many believe that a dedicated JSONB style column would offer better performance. Others want support for patching JSON data instead of having to replace the document as a whole.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

SQL Server by Kevin O'Brien

Good points you shared and great observation in the industry to add JSON Support with SQL Server it's a very good approach.

SQL Daddy by SQL Daddy

I have read this post and its really relevant and informative. I have also researched more forum,blog and website and got answer here that "how to load JSON file to SQL server". You can also visit here for detail.
binaryworld.net/blogs/consuming-json-data-in-sq...

Json is the future by James Malvi

JSON is everywhere and mongodb is all in for JSON. JS suppots is as defalt and mobile app is using rest api and json is faster ,shorted and very easy to understand and everyone will go basaltic about json. I would recommends many tools which will help to JSON lovers codebeautify.org/jsonviewer and jsonformatter.org

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

3 Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT