You want JSON? You finally have JSON support within SQL Server! The much-asked-for, long-awaited feature is finally here! In this session, Davide will show how the JSON support works within SQL Server, what are the pros and cons, the capabilities and the limitations, and will also take a look at performance of JSON vs. an equivalent relational(ish) solution to solve the common “unknown-schema-upfront” and “I-wanna-be-flexible” problems.
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
SQL Server 2016 JSON
1. SQL Server 2016 JSON
Davide Mauri
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
2. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
About Me
Microsoft SQL Server MVP
Works with SQL Server from 6.5, on BI from 2003
Specialized in Data Solution Architecture, Database Design, Performance
Tuning, High-Performance Data Warehousing, BI, Big Data
President of UGISS (Italian SQL Server UG)
Regular Speaker @ SQL Server events
Consulting & Training, Mentor @ SolidQ
E-mail: dmauri@solidq.com
Twitter: @mauridb
Blog: http://sqlblog.com/blogs/davide_mauri/default.aspx
3. 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”)
4. 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
5. 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-
server/
6. 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:
7. 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
8. 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 path
handling
• LAX: null
• STRICT: error
$.conference.speaker
$.conference.speaker.editions[0]
9. 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
10. 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
11. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
DEMO
SQL/JSON First Contacts
12. 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
13. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
OPENJSON
• Support both implicit and explicit schema:
Type Meaning
0 null
1 string
2 int
3 true/false
4 array
5 object
14. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
OPENJSON
• Very useful with OPENROWSET to import JSON
15. 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
16. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
DEMO
JSON and Relations
17. 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)
18. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
DEMO
JSON Indexing
19. 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
20. 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)
21. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Words of Wisdom
«Schemaless => implicit schema = bad.
Prefer an explicit schema»
(Schemaless data structures, Martin Fowler)
22. 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
23. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
DEMO
Dynamic Schema
24. 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-
node-js-to-SQL-Server
• JSON is also supported on SQL Azure
• Requires server version V12
25. 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
26. 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
27. 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
28. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Thanks!
Questions?
29. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Demos available on GitHub
https://github.com/yorek/devweek2016