Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon

No Scalars with JSON_QUERY–#SQLNewBlogger from Blog Posts - SQLServerCentral

Save for later
  • 2 min read
  • 16 Dec 2020

article-image

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

I started to dig into JSON queries recently, and as I continued to experiment with JSON, this struck me as strange. Why is there a NULL in the result?

no-scalars-with-json_query-sqlnewblogger-from-blog-posts-sqlservercentral-img-0

The path looks right. This appears to be somewhere I ought to get a result back. As I looked up the JSON_QUERY documentation, and it says I get an object or array back. I’d somewhat expect that position, while containing a single value, could be seen as an object of

{“setter”}

The fact that I need to know I have a single value here seems like poor design. If the document changes, perhaps someone might enter this:

DECLARE @json NVARCHAR(1000)
     = N'
  {  "player": {
              "name" : "Sarah",
              "position" : "setter, DS"
             },
    "team":"varsity"
  }
';

In this case, a JSON_VALUE would fail, while a JSON_QUERY wouldn’t work in the first example above. This means that I need to modify my code based on documents.

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime

I don’t like this, but I need to know this, so if you work with JSON, make sure you know how the functions work.

SQLNewBlogger

While writing the previous post, I changed one of the function calls and got the NULL. I had to fix things for the other post, but I kept the query and then spent about 10 minutes writing this one to show a little thought into the language.

You can easily take something you are confused about, made a mistake doing, or wonder about and write your own post.

The post No Scalars with JSON_QUERY–#SQLNewBlogger appeared first on SQLServerCentral.