There is enough to cover that I will split this topic into multiple blog posts. This is post number one, where I will cover how to store JSON in your tables and how to interact with scalar values. The other posts are (will be updated as I go):
You can find the other parts of the series here (I'll update as I go)
- Accessing scalar properties on JSON objects stored in SQL Server (this post)
- Accessing array data in JSON objects stored in SQL Server
Also, if you want to recreate the database I have here and try the samples out for yourself, you can find everything you need on Github ar:
So lets take a look at what we can do.
Storing JSON Data in a Table
SQL Server does not contain a dedicated data type for JSON data, you simply just store your data in a VARCHAR (or NVARCHAR) data type of the appropriate size.
Below is a table that I have defined that is going to hold some weather data.
Our JSON will be stored in the column named ObservationData. The data type for this field is simply a VARCHAR(4000) field, which is sufficient to hold the JSON objects we''l be storing in it. If you have larger objects, you can use a VARCHAR(MAX) field, and indeed we'll se an example of this later.
This is a sample of the JSON we'll be storing in this column.
So we see we have a number of weather related properties off of the main object and a nested object for the location. Our data in this column may be for different cities, but it all has this same format, which sets us up for the next step, querying the data.
Querying Scalar Values in JSON Data
First lets look at if we run a plain old SQL Query what we get back. Here is our initial query:
And our results:
We can clearly see there is JSON stored in the ObservationData column. But now we want to do something with it. Lets pull out the temperature and humidity readings from each observation and make those columns in our result set.
To do this, we use a new function in SQL Server called JSON_VALUE. JSON_VALUE takes two arguments:
- A JSON expression, which is typically a the name of a column that contains JSON text, but could also be a T-SQL variable containing JSON.
- A path expression, which describes how to navigate to the scalar value you want to extract out of the JSON.
So now, we are going to rewrite our query to extract these fields out as columns like this.
And here are our results.
We see that SQL Server used the JSON_VALUE function to dynamically pull those values out of our JSON so we can see those as columns in our result set. And now we can interact with them like we would any other column in a table. So we can keep this data stored as JSON, but interact with it in a relational manner when that makes sense, which is pretty cool. Traditionally, we would have needed an ETL process that would have parsed this data out and placed each element in a column, but now we can do that on the fly.
The Power of Virtual (Computed) Columns
With our weather data, it would be pretty cool if I didn't have to write that syntax each time I queried the table but could just get a couple of these key fields like temperature and humidity. By combining the JSON_VALUE function with SQL Server's computed columns feature, we can create a set of virtual columns on our table which will do just that, pulling the data out of the JSON each time so it appears as just another column on the table. Here is what the syntax looks like.
Now, if we enter our original SELECT * query against the table, we will see that these two columns just show up in our result set like any other columns. And of course, we could also refer to them by name, either in a SELECT clause, WHERE clause or JOIN condition. The function just like any other column, even though what is happening is real time, SQL Server is parsing the JSON and retrieving this value for us.
That is an important point if we want to use these columns in a WHERE clause or a JOIN condition. We know that for a table of any size, if we query the table by a column that is not indexed, our performance is going to be very slow because SQL Server has to read all of the rows of the table from disk and find the columns we are looking for. Here, our results would probably be even worse if we tried to filter by one of these JSON virtual columns, because not only is SQL Server going to have to scan the whole table, it will also have to parse the JSON for each row. Not good!
Indexing Computed Columns
However, SQL Server allows you to create an index over a computed (virtual) column. When you do this, the values of the computed columns are computed at index creation time and then persisted in the index so they can be searched quickly like values of any other column. In this way, but using the JSON_VALUE function in conjunction with a computed column and an index over that computed column, we can quickly search values in our JSON data like we would any other column in our table. Lets look at an example.
Imagine we redefined our table from above to be about as simple as could be, a surrogate key ObservationId for the primary key and a column to hold our JSON data so that our CREATE TABLE statement now looks like this.
This is about as simple as storing data can get. All of our data is inside the ObservationData column. This includes fields that we will probably want later in order to look the data up by, things like the observation time, the city, the state and the station code. We could write a query like this...
Not exactly the paragon of simplicity. Further, the execution plan confirms, this query is doing a full table scan to go and find the data, so it is both slow and resource intensive.
So first we are going to create some virtual columns to one, make the table easier to work with and two, so that we can create an index in the next step.
And now for the indexes.
When you create these indexes, you will get a warning that you may be exceeding the maximum non-clustered index key size, because theoretically you could be pulling a very large string out of the JSON. If you have larger strings, you want to make sure that these will fit under the 1700 byte limit for the key size of an index, and in our case, we are well below that, so we are fine to continue.
So now, we can rewrite our query so it looks like this:
This is much simpler and performs much better, as now the query is using one of our indexes. So again, pretty cool that we can not just reach into the JSON to grab the values out, but we can also index any key properties we have so we can quickly search through the data that we have.
So there are the basics of storing JSON in SQL Server and interacting with scalar values. in part 2, we'll take a look at interacting with arrays that may be stored in your JSON data.