SQL Server 2016 JSON

29
SQL Server 2016 JSON Davide Mauri Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

Transcript of SQL Server 2016 JSON

Page 1: SQL Server 2016 JSON

SQL Server 2016 JSON

Davide Mauri

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

Page 2: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

About MeMicrosoft SQL Server MVPWorks with SQL Server from 6.5, on BI from 2003Specialized in Data Solution Architecture, Database Design, Performance Tuning, High-Performance Data Warehousing, BI, Big DataPresident of UGISS (Italian SQL Server UG)Regular Speaker @ SQL Server eventsConsulting & Training, Mentor @ SolidQE-mail: [email protected]: @mauridb Blog: http://sqlblog.com/blogs/davide_mauri/default.aspx

Page 3: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

Agenda• JSON in life• JSON and SQL Server

• JSON “Datatype” (not really) • Navigation via “dot” notation• LAX and STRICT path modes

• JSON Functions• JSON_VALUE, JSON_QUERY, JSON_MODIFY, ISJSON

• JSON & SQL• OPENJSON• FOR JSON• Indexes

• The Dynamic Schema problem (or “the Relational Division”)

Page 4: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

JSON in Life• That’s a DEV conference….• …so I’m sure you know JSON pretty well • For who doesn’t already know it:

• JavaScript Object Notation• Simple, Human-Readable, Text data format• Based on Key-Value pair idea

• Values can be: Scalar, Arrays, Key-Value pairs• No strict schema (yet)• Supported in any language, cross platform• De-facto standard

• http://www.ietf.org/rfc/rfc4627.txt Image taken from wikipedia

Page 5: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

JSON and SQL Server• No support until SQL Server2016• Before SQL Server 2016:

• One option is to use SQLCLR• Solutions available surfing the web:• http://www.sqlservercentral.com/articles/SQLCLR/74160/ • http://www.json4sql.com/examples.html

• Another (more limited) option is a pure T-SQL solution• https://www.simple-talk.com/sql/t-sql-programming/consuming-json-strings-in-sql-serv

er/

Page 6: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

JSON and SQL Server• Why native JSON support it’s important?

• Simplify application development• Especially in non-MS environments, since JSON is *very* well supported (eg. Python / JS)

• Reduces the impedance mismatch between app and db• Make schema extensibility a breeze

• But without having *only* “liquid” schemas. Schema is a good thing in general. Having the option to decide when and when not use it is a key feature today:

Page 7: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

JSON and SQL Server 2016• Native support to JSON• No specific datatype format (like XML)

• relies on varchar(max)

• Specific function to • manipulate and create JSON• extract data from JSON• turn relational data into JSON and vice-versa

Page 8: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

JSON Path• The dollar sign ($) represents the context item.• The property path is a set of path steps.

• Key names. • Array elements. Arrays are zero-based.• The dot operator (.) indicates a member

of an object.

• “LAX” or “STRICT” option set invalid pathhandling

• LAX: null• STRICT: error

$.conference.speaker

$.conference.speaker.editions[0]

Page 9: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

JSON Path• Like XPATH but for JSON

• http://goessner.net/articles/JsonPath• https://jsonpath.curiousconcept.com• http://jsonpath.com

• SQL Server 2016 doesn’t offer a full support yet.

• Is a subset of JSONPath

Page 10: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

JSON Functions• Scalar functions to manage JSON data

• ISJSON: test if string is JSON• JSON_VALUE: extract a scalar value• JSON_QUERY: extract a JSON value• JSON_MODIFY: modify a JSON property

Page 11: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

DEMOSQL/JSON First Contacts

Page 12: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

JSON Functions• Tabular functions

• OPENJSON: turn a JSON object into a table• FOR JSON: turn a table into a JSON object

Page 13: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

OPENJSON• Support both implicit and explicit schema:

IMPLIC

ITEXPLICIT

Type Meaning

0 null

1 string

2 int

3 true/false

4 array

5 object

Page 14: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

OPENJSON• Very useful with OPENROWSET to import JSON

Page 15: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

FOR JSON• Two modes: AUTO, PATH

• AUTO: shape of JSON using the SELECT statement as guideline

• PATH: column alias and JSON_QUERY function allows for total control of final JSON

Page 16: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

DEMOJSON and Relations

Page 17: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

JSON Indexes• JSON data support indexes…even if it doesn’t seems so!

• Right, there is no special index like it happens with XML

• How to index JSON Data• Create calculated columns for scalar JSON values you know if want to index• Create FULLTEXT index to index whole JSON document• Use the FULLTEXT index to perform “near” term search

• CONTAINS(JSON_DATA, ‘NEAR((“Key”, “Value”),1)’)• Uses a “Generalized Inverted Index” (like Postgres)

• https://msdn.microsoft.com/en-us/library/cc879306.aspx

• Indexes to support JSON Path queries (like XML) are not yet there• You have to DIY here (Service Broker to async update EAV table)

Page 18: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

DEMOJSON Indexing

Page 19: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

Dynamic Schema• JSON sounds like a perfect solution of all the “Dynamic Schema”

situations• Why “Dynamic Schema” and not Schemaless?

• Because schemaless does not *really* exists! In reality a schema always exists, albeit implicit, otherwise it would be impossible to handle data

Page 20: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

Implict Schema

Any data that doesn't fit this implicit schema will not be manipulated properly, leading to errors.

(Schemaless data structures, Martin Fowler)

Page 21: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

Words of Wisdom

«Schemaless => implicit schema = bad. Prefer an explicit schema»

(Schemaless data structures, Martin Fowler)

Page 22: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

Dynamic Schema• What if my use case is one that perfectly fits the need for a implicit

schema?• The only possible solution are the so-called «No-SQL» databases

• Document Database or Key-Value store?• How can I integrate it into already existing database?• Integration does not come for free!

• Now with SQL Server 2016 we have an additional option! • No integration problems, No added complexity• Great Performances• Works within a well-known platform

Page 23: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

DEMODynamic Schema

Page 24: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

Last things• JSON and the new COMPRESS function works well together:

• Compress using GZIP algorithm (Natively supported by browsers)• Simplify app development and integration with rdbms• Sample usage with node.js• http://www.codeproject.com/Articles/1063475/Pushing-Compression-from-n

ode-js-to-SQL-Server

• JSON is also supported on SQL Azure• Requires server version V12

Page 25: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

Conclusions• JSON does not substitute relational solutions

• Can be useful to extend them• Can be *very* useful to simplify app-to-db communications

• JSON as parameter value! That’s a dream • FAQ:

• https://msdn.microsoft.com/en-us/library/mt631706.aspx

• Follow SQL Server JSON PM:• https://blogs.msdn.microsoft.com/sqlserverstorageengine/tag/json

Page 26: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

Help us to make JSON support better!• Don't restrict JSON_VALUE and JSON_QUERY to string literals only

for the path• https://connect.microsoft.com/SQLServer/Feedback/Details/2235470

• Support JSON in COLUMNSET for SPARSE columns• https://connect.microsoft.com/SQLServer/feedback/details/2533991

Page 27: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

Help us to make JSON support better!• Allow support for JSON of simple arrays generation

• https://connect.microsoft.com/SQLServer/Feedback/Details/1383569

• JSON_VALUE: adding another optional parameter, which defines what type of value the function will return

• https://connect.microsoft.com/SQLServer/Feedback/Details/2543881

Page 28: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

Thanks!Questions?

Page 29: SQL Server 2016 JSON

Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek

Demos available on GitHubhttps://github.com/yorek/devweek2016