Download presentation
Presentation is loading. Please wait.
1
JSON for the Data Mortal
SQL Saturday #784 Sioux Falls, SD
2
Who Am I? Jim Dorame Jim Dorame Director of Data & Reporting
Working with Microsoft SQL Server since 1995 Jim Dorame Twitter.com\dbajd
3
Agenda Goals What is JSON Conclusion XML vs. JSON JSON in SQL Server
4
Goals What is JSON? XML vs. JSON.
Implementation of JSON in SQL Server. How you can use JSON.
5
Agenda Goals What is JSON Conclusion XML vs. JSON JSON in SQL Server
6
JSON
7
JSON
8
JSON
9
JSON What is JSON? JSON has been around since 1999
Based off JavaScript 3rd edition JavaScript Object Notation (JSON) Data-interchange format
10
JSON What is JSON? JSON based on 2 structures
Name/Value pairs of data Order list of data (arrays) Any programming language can use JSON A way to pass data to and from apps, web pages, etc. Programming Languages Visual BASIC C# JAVA PASS Data ETC. = Restul API, web Services
11
JSON Where is JSON being used? Web applications
Databases that support JSON Postgress MySQL MongoDB (and many other document dbs) Oracle SQL Server (2016+)
12
Agenda Goals What is JSON Conclusion XML vs. JSON JSON in SQL Server
13
XML vs JSON XML <Books> <book>
<Title>Pro SQL Server 2012 Practices</Title> <Author>Jes Borland</Author> </book> <Title>Expert Performance Indexing for SQL Server 2012</Title> <Author>Jason Strate</Author> </Books>
14
XML vs JSON XML Hierarchical Elements (Tags)
Elements can have attributes Elements can be nested as children Values (Text of the elements) Self describing Extremely verbose
15
XML vs JSON JSON { "Books": [
"Title": "Pro SQL Server 2012 Practices", "author": "Jes Borland" }, "Title": "Expert Performance Indexing for SQL Server 2012", "author": "Jason Strate" } ]
16
XML vs JSON JSON Hierarchical Squiggly brackets ‘containers’ (objects)
Square brackets are arrays Names and values separated by colon (members) Items of an array separated by commas Self describing Not as verbose as XML(no need for “closing tag”, just name it)
17
XML vs JSON JSON and XML have similarities
JSON easier for human to create and read JSON has arrays JSON and XML are both in SQL Server JSON and XML have similarities, however there are differences that set JSON apart from XML
18
Agenda Goals What is JSON Conclusion XML vs. JSON JSON in SQL Server
19
JSON and SQL Server JSON Functions FOR JSON OPENJSON INDEXING
How is JSON supported in SQL SERVER? JSON Functions FOR JSON OPENJSON INDEXING
20
JSON and SQL Server JSON Functions ISJSON(expression)
Checks to ensure the expression is JSON Returns 1 if valid JSON else 0 JSON_Value(expression, path) Return a text value type nvarchar(4000) Given a JSON string, and a starting location return the requested text, if text requested is JSON array then NULL or error is returned
21
JSON and SQL Server JSON Functions JSON_QUERY (expression, path)
Return JSON fragment type nvarchar(max) If the requested data is not a JSON array null or error will be returned JSON_Modify(expression, path, newValue) Can add, delete or update JSON in the given expression Returns the updated value of the expression as properly formatted JSON
22
JSON and SQL Server STRING_ESCAPE Syntax STRING_ESCAPE(text, type)
Text – nvarchar representing object being escaped Type – Escaping rules to be applied currently only JSON Returns nvarchar(max) with escaped special and control characters (JSON based only) This is the one function that needs to have DB compatibility set to 130.
23
JSON and SQL Server STRING_ESCAPE Special character Encoded sequence
Quotation mark (") \" Reverse solidus (\) \\ Solidus (/) \/ Backspace \b Form feed \f New line \n Carriage return \r Horizontal tab \t This is the one function that needs to have DB compatibility set to 130.
24
JSON and SQL Server JSON Functions Demo Demo
25
JSON and SQL Server FORJSON
Use PATH mode to maintain full control over format of JSON output Use AUTO mode formatted based on structure of select statement. 3 options ROOT Add a single top-level element to the JSON without this no root element Include null Values this will include JSON properties for nulled results WITHOUT_ARRAY_WRAPPER this removes the square brackets from the JSON by default the square brackets are there
26
JSON and SQL Server FORJSON Demo Demo
27
JSON and SQL Server OPENJSON Convert JSON data to Rows and Columns.
Only available under compatibility 130. Can use default schema or explicit schema. Return values when using the default schema Key nvarchar(4000) -- name of the property or index Value nvarchar(max) -- value of the property Type int -- contains the type of the value only returned with default schema This is the one function that needs to have DB compatibility set to 130.
28
JSON and SQL Server OPENJSON Return values continued
Value of the Type column JSON data type Null 1 String 2 Int 3 True/False 4 Array 5 Object This is the one function that needs to have DB compatibility set to 130.
29
JSON and SQL Server OPENJSON Default schema syntax
OPENJSON( jsonExpression [ , path ] ) Explicit schema syntax WITH ( colName type [ column_path ] [ AS JSON ] [ , colName type [ column_path ] [ AS JSON ] ] [ , n ] ) This is the one function that needs to have DB compatibility set to 130.
30
JSON and SQL Server OPENJSON Demo Demo
31
JSON and SQL Server INDEXING Yes you can index JSON
Must create a computed column on the value Create index on virtual column as normal index JSON indexes are collation aware. BOL “The result of the JSON_VALUE function is a text value that inherits its collation from the input expression” Computed column is based of a JSON_VALUE function returning that data element for indexing.
32
JSON and SQL Server INDEXING Demo Demo
33
Agenda Goals What is JSON Conclusion XML vs. JSON JSON in SQL Server
34
Concluding Remarks
35
Resources JSON Data (SQL Server) JSON JSON Tutorial
JSON JSON Tutorial Online JSON formatter and validator
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.