T-SQL Tuesday #081: Getting Sharper

Datetime:2016-08-23 02:28:07          Topic: SQL           Share

Sharpen Something

This month I am the host of the TSQL Tuesday blog party. In the invite, which can be read here , I asked people to decide on something to work on, plan out and then report the success/failure.

Not only am I the host, but I am a member this month. In my invite (and the reminder) I provided a few examples of what I was really looking for from participants this month. It became apparent that the topic may have been over thought. So, for my contribution, I decided to do something extremely simple.

There is so much about SQL Server that it would not be feasible nor should it be expected that one single person should know everything about the product. That said, within SQL Server alone, all of us have something to learn and improve upon within our skill-set. If we extend out to the professional development realm, we have even more we can explore as a skill sharpening experiment for this month.

I am going to keep it strictly within the SQL Server realm this month. I have chosen to develop my skills a little more with the topic of JSON. I should be an expert in JSON, but since it is spelled incorrectly – maybe I have something to learn. That said, I really do love being in the database now – haha.

JSON

Let’s just get this out there right now – I suck at JSON. I suck at XML. The idea of querying a non-normalized document to get the data is not very endearing to me. It is for that reason that I have written utilities or scripts to help generate my XML shredding scripts – as

can be seen here .

Knowing that I have this allergy to features similar to XML, I need to build up some resistance to the allergy through a little learning and a little practice. Based on that, my plan is pretty simple:

  1. Read up on JSON
  2. Find some tutorials on JSON
  3. Practice using the feature
  4. Potentially do something destructive with JSON

With that plan set before me, it is time to sharpen some skills and then slice, dice, and maybe shred some JSON.

Sharpening

Nothing in this entire process was actually too terribly difficult. That is an important notion to understand. My plan was very lacking in detail and really just had broad strokes. This helps me to be adaptable to changing demands and time constraints. I dare say the combination of broad strokes and a very limited scope also allowed me an opportunity for easier success.

Researching JSON was pretty straight forward. This really meant a few google searches. There was a little bit of time spent reading material from other blogs, a little bit from BOL and a little bit from msdn. Nothing extravagant here. I did also have the opportunity to review some slides from a Microsoft presentation on the topic. Again, not terribly difficult or demanding in effort or time requirement. This research covers both steps one and two in the plan.

Now comes the more difficult task. It was time to put some of what had been seen and read to practice. A little experimentation was necessary. I have two easy enough looking examples that I was able to construct to start experimenting with in my learning endeavors.

Here is the first example. This is a bit more basic in construct.

declare  @json NVARCHAR(4000)
SET @json 
= N'[
{
"Order": {
"Number":"8675",
"Date":"2016-07-31T00:00:00"
},
"Account": "Jenny",
"Item": {
"Price":59.99,
"Quality":1
}
},
{
"Order":{
"Number":"309",
"Date":"2016-06-01T00:00:00"
},
"Account": "I Got It",
"Item":{
"Price":24.99,
"Quality":10
}
}
] '
 
 
 
select *
 From openjson(@json)
 with (Number  varchar(200) N'$.Order.Number'
 ,Date datetime    N'$.Order.Date'
 ,Customer varchar(200) N'$.Account'
 ,ConsumerSatisfactionLevel int N'$.Item.Quality')
GO

And some basic results:

Pretty slick. Better yet is that this is many times easier than XML.

How about something a little different like the following:

declare  @json NVARCHAR(4000)
SET @json 
= N'{"DBA":[
   { "name": "Jason"
   ,"surname":null
   ,"skills":["SQL","XE","Azure"] }
   , { "name": "J", "surname": "SON", "skills":null } ]}
'
 
select *
 From openjson(@json, N'$.DBA')
 with (Name varchar(32) N'$.name', LastName Varchar(32) N'$.surname'
 ,Skill1 varchar(20) N'$.skills[0]'
 ,Skill2 varchar(20) N'$.skills[1]'
 ,Skill3 varchar(20) N'$.skills[2]'
 )
 ;
GO

Admittedly, this one is a bit more of a hack. In my defense, I am still learning how to work with this type of stuff. At any rate, I had an array of values for one  of the attributes. The kludge I used reads up to 3 values from that array and returns those values into individual attributes. I am still learning in this area so I can live with this for now.

The last part of the plan involved doing something destructive. Why? Well just for the fun of it. I was unable to get to this stage but it is still in the plans.

Report on The Successes and Failures

I have written about some of the successes and failures along the way thus far. Overall, I would rate this a successful endeavor. The big reason for it being a success is because I do feel I learned more about json within SQL Server than I had prior to the experiment.

Taking a bite sized chunk of learning and acting on it sure makes it a lot easier to learn a new concept or to learn more about such a vast topic such as SQL Server.

*Note: This is a late publish because the post didn’t auto post. This is a tad late but I discovered it as I was prepping the roundup.





About List