A JSON’s Journey through SQL Server

Slides:



Advertisements
Similar presentations
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: Introduction to Database Management, All Rights ReservedIntroduction to Database Management.
Advertisements

SQL Server 2000 and XML Erik Veerman Consultant Intellinet Business Intelligence.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
Using XML in SQL Server 2005 NameTitleCompany. XML Overview Business Opportunity The majority of all data transmitted electronically between organizations.
ASP.NET Programming with C# and SQL Server First Edition
DBA Developer. Responsibilities  Designing Relational databases  Developing interface layer Environment Microsoft SQL Server,.NET SQL Layer: Stored.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
XML in SQL Server Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Chapter 8 Cookies And Security JavaScript, Third Edition.
5/24/01 Leveraging SQL Server 2000 in ColdFusion Applications December 9, 2003 Chris Lomvardias SRA International
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
Module 18 Querying XML Data in SQL Server® 2008 R2.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Data Types Lesson 4. Skills Matrix Table A table stores your data. Tables are relational in that they are organized as rows and columns (a matrix). Each.
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
Session 11 Creating Tables and Using Data Types. RDBMS and Data Management/Session 11/2 of 40 Session Objectives Define the data types and list the categories.
Visual Programing SQL Overview Section 1.
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
Module 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.
What is your Character Data Type? March 5, 2016 John Deardurff Website:
Notice: MySQL is a registered trademark of Sun Microsystems, Inc. MySQL Conference & Expo 2011 Michael “Monty” Widenius Oleksandr “Sanja”
Introduction to Mongo DB(NO SQL data Base)
Web Database Programming Using PHP
Creating Database Objects
SQL Server getting closer to
With Temporal Tables and More
All about JSON Scenarios and value proposition for JSON data enabled in Azure SQL Database and SQL Server Ralph Kemperdick Digital Business Architect,
Developing modern applications with Temporal Tables and JSON
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Relational Database Design
Module 2: Creating Data Types and Tables
MongoDB Er. Shiva K. Shrestha ME Computer, NCIT
Lecture 6 Data Model Design (continued)
Making Developers lives easier with SQL Server 2016
Web Database Programming Using PHP
Data Definition and Data Types
Lesson 7 Managing Data Creating a database with Web Matrix.
Building REST API using SQL Server and JSON Functions
Attributes and Domains
Making Developers lives easier with SQL Server 2016
Ouch! Our Data Type Choices Did THAT?
Twitter & NoSQL Integration with MVC4 Web API
SQL Server 2016 JSON Support FOR Data Warehousing
14 T-SQL Functions You May Not Know
Migrating a Disk-based Table to a Memory-optimized one in SQL Server
Proper DataType Usage = Guaranteed Better Performance and Accuracy
DATABASE MANAGEMENT SYSTEM
{. “speaker”:. “Emanuele Zanchettin”,. “session” :
What is your Character Data Type?
Working with Data Types
Intro to NoSQL Databases
Database systems Lecture 2 – Data Types
JSON Processing in SQL Server (Why I hate XML)
PT2520 Unit 5: Physical Design
What’s new with SQL Server
Intro to NoSQL Databases
JSON for the Data Mortal
14 T-SQL Functions You May Not Know
Integrating REST API and SQL Server JSON Functions
Attributes and Domains
Chapter 2: Creating And Modifying Database Tables
Chapter 11 Managing Databases with SQL Server 2000
Structured Query Language Path from Unorganized to Organized….
Objectives In this lesson, you will learn to:
Creating Database Objects
Intro to NoSQL Databases
JDBC II IS
Introduction to SQL Server and the Structure Query Language
XML? What’s this doing in my database? Adam Koehler
Presentation transcript:

A JSON’s Journey through SQL Server Shamil Sadique (MCSE, OCA) Senior DBA – Pearson Lanka (Pvt) Ltd. https://www.linkedin.com/in/shamilsaleem @shamilsaleem A JSON’s Journey through SQL Server

JSON Overview

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

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

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

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

Native JSON Support

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.

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

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

Native JSON Support Table data to JSON text JSON text to table data Built in JSON functions https://docs.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server

Business Case : “Smart University” Database

TABLE DATA TO JSON TEXT

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

Demo : FOR JSON

Converting JSON text to table data

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

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

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

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 ] ) ]

Demo : OPENJSON

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

Demo : Other Uses of OPENJSON

Extracting Specific Values

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.

Demo : JSON_VALUE & JSON_QUERY

VALIDATING JSON DATA

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.

Demo : isJSON

PERFORMANCE CONSIDERATIONS

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.

Demo : Indexing Computed Columns

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 * https://blog.bertwagner.com/xml-vs-json-shootout-which-is-superior-in-sql-server-2016-b97bf7766ef2

QUESTIONS ?

Thank you !

References and Further Reading : Stacia Varga, Denny Cherry, Joseph D’Antoni (2016). Introducing Microsoft SQL Server 2016 Mission-Critical Applications, Deeper Insights, Hyperscale Cloud. Microsoft Press William Durkin, Miloš Radivojević, Dejan Sarka (2017). SQL Server 2016 Developer's Guide. Packt Publishing https://blog.bertwagner.com/xml-vs-json-shootout-which-is-superior-in-sql-server-2016-b97bf7766ef2