Presentation is loading. Please wait.

Presentation is loading. Please wait.

A JSON’s Journey through SQL Server

Similar presentations


Presentation on theme: "A JSON’s Journey through SQL Server"— Presentation transcript:

1 A JSON’s Journey through SQL Server
Shamil Sadique (MCSE, OCA) Senior DBA – Pearson Lanka (Pvt) Ltd. @shamilsaleem A JSON’s Journey through SQL Server

2 JSON Overview

3 What is JSON JavaScript Object Notation (JSON) an open standard format for data exchange between applications Commonly used in web applications, and NoSQL datastores such as MongoDB and CouchDB Language independent and flexible, hence have become popular in the industry

4 The JSON Object Consists of key-value pairs Values separated by comma
Values can be String Number True/false Objects (arrays / nested json objects)

5

6 Road to JSON Support in SQL Server
Feature was Requested to Microsoft Connect by the community in 2011 Was one of the most popular feature request until it was implemented

7

8 JSON Prior to SQL Server 2016
CLR-based solutions DLL libraries to extend support into reading JSON data. TSQL-based solutions UDFs written to decode JSON data. Both being External Tools and Approaches

9 Native JSON Support

10 Storing JSON in SQL Server
Native support introduced in SQL Server 2016 No special JSON datatype, but uses “nvarchar” due to : Backward compatibility Enable old applications/codebase to integrate with this feature Cross feature compatibility Get the support of all sql server features which are compatible with nvarchar. (memory-optimized tables, temporal tables, row level security) Client side support Client tools outside sql server represent JSON as string.

11 Disadvantages of not having a special datatype :
Performance could’ve been improved Optimized index types would've been introduced.

12 Mapping between SQL server and JSON data types:
Category SQL Data Type JSON Data Type Character char, nchar varchar, nvarchar string Numeric int, bigint, float, decimal numeric number Bit bit true/false Date and Time date, datetime, datetime2, time, datetimeoffset Binary binary BASE64-encoded string Other uniqueidentifier, money

13 Native JSON Support Table data to JSON text JSON text to table data
Built in JSON functions

14 Business Case : “Smart University” Database

15 TABLE DATA TO JSON TEXT

16 FOR JSON FOR JSON AUTO PATH Automatically formats results into JSON
Nesting based on table hierarchy Less control over the final result Options Include : ROOT Creates a single root object INCLUDE_NULL_VALUES Don’t generate [] syntax SQL Server normally adds [ ] assuming the query will return more than 1 row. Full Control over the final results Ability to nest / change the level of the resulting JSON

17 Demo : FOR JSON

18 Converting JSON text to table data

19 OPENJSON Parses JSON text and returns the properties as rows and columns Can be used in the FROM clause of a TSQL statement. Available only under compatibility level 130 or higher

20 OPENJSON DEFAULT SCHEMA CUSTOMIZED SCHEMA
Always returns a resultset containing 3 columns. The Columns can be customized Additional “WITH” clause is used.

21 OPENJSON WTH DEFAULT SCHEMA
Only returns first level properties Nested properties will be separate JSONs A Resultset consists of 3 columns is returned. Key Name of the JSON property Value Value of the JSON property; nulls are allowed on this column datatype is nvarchar(max) Type The datatype of the value Column only holds tinyint values Value of the “Type” Column Corresponding JSON Data Type Null 1 String 2 Number 3 True/false 4 Array 5 Object

22 OPENJSON WITH CUSTOMIZED SCHEMA
Used to get a customized resultset with complex values. Used the “WITH” clause in the OPENJSON function. WITH clause maps the JSON attributes to the column names: OPENJSON( jsonExpression [ , path ] ) [ WITH ( column_name data_type [ column_path ] [ AS JSON ] [ , column_name data_type [ column_path ] [ AS JSON ] ] [ , n ] ) ]

23 Demo : OPENJSON

24 Other uses of OPENJSON Transpose Comma Separated Values to a Column.
Compare data among rows.

25 Demo : Other Uses of OPENJSON

26 Extracting Specific Values

27 Extracting specific values form JSON
JSON_VALUE Returns a scalar value from a JSON object. JSON_QUERY Returns an object or array value from a JSON object.

28 Demo : JSON_VALUE & JSON_QUERY

29 VALIDATING JSON DATA

30 isJSON Validates a JSON
Can be used in a check constraint validating a column which is supposed to insert JSON data. No Native datatype for JSON, so this is important.

31 Demo : isJSON

32 PERFORMANCE CONSIDERATIONS

33 Indexing JSON Data Columns
Stored as text; no special datatype; hence no special indexes. Ways to improve performance : Create computer columns based on heavily using JSON tags and Index those Computed Columns Utilize in full-text indexes Hence, not recommended for large amounts of data.

34 Demo : Indexing Computed Columns

35 XML vs JSON in SQL Server
XML in SQL Server JSON in SQL Server Support introduced in SQL Server 2005 Introduced in SQL Server 2016 Has a dedicated datatype and indexes No dedicated datatype, stored in nvarchar Smaller footprint compared to the same data represented as JSON* Larger footprint to save the same data compared to xml. Automatically validates if inserted to a xml column* No automatic validation as the datatype is plain nvarchar XML Data retrieval is slower compared to JSON* XMLDATA.VALUE, XMLDATA.QUERY Data retrieval is faster JSON_VALUE, JSON_QUERY *

36 QUESTIONS ?

37 Thank you !

38 References and Further Reading :
Stacia Varga, Denny Cherry, Joseph D’Antoni (2016). Introducing Microsoft SQL Server Mission-Critical Applications, Deeper Insights, Hyperscale Cloud. Microsoft Press William Durkin, Miloš Radivojević, Dejan Sarka (2017). SQL Server 2016 Developer's Guide. Packt Publishing


Download ppt "A JSON’s Journey through SQL Server"

Similar presentations


Ads by Google