SQL Server 2016 JSON
Davide Mauri
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
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
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”)
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
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/
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:
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
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]
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
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
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
DEMO
SQL/JSON First Contacts
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
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
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
OPENJSON
• Very useful with OPENROWSET to import 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
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
DEMO
JSON and Relations
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)
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
DEMO
JSON Indexing
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
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)
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Words of Wisdom
«Schemaless => implicit schema = bad.
Prefer an explicit schema»
(Schemaless data structures, Martin Fowler)
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
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
DEMO
Dynamic Schema
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
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
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
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
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Thanks!
Questions?
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Demos available on GitHub
https://github.com/yorek/devweek2016

SQL Server 2016 JSON

  • 1.
    SQL Server 2016JSON Davide Mauri Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
  • 2.
    Join the conversationon 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 conversationon 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 conversationon 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 conversationon 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 conversationon 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 conversationon 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 conversationon 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 conversationon 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 conversationon 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 conversationon Twitter: @DevWeek // #DW2016 // #DevWeek DEMO SQL/JSON First Contacts
  • 12.
    Join the conversationon 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 conversationon 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 conversationon Twitter: @DevWeek // #DW2016 // #DevWeek OPENJSON • Very useful with OPENROWSET to import JSON
  • 15.
    Join the conversationon 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 conversationon Twitter: @DevWeek // #DW2016 // #DevWeek DEMO JSON and Relations
  • 17.
    Join the conversationon 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 conversationon Twitter: @DevWeek // #DW2016 // #DevWeek DEMO JSON Indexing
  • 19.
    Join the conversationon 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 conversationon 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 conversationon Twitter: @DevWeek // #DW2016 // #DevWeek Words of Wisdom «Schemaless => implicit schema = bad. Prefer an explicit schema» (Schemaless data structures, Martin Fowler)
  • 22.
    Join the conversationon 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 conversationon Twitter: @DevWeek // #DW2016 // #DevWeek DEMO Dynamic Schema
  • 24.
    Join the conversationon 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 conversationon 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 conversationon 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 conversationon 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 conversationon Twitter: @DevWeek // #DW2016 // #DevWeek Thanks! Questions?
  • 29.
    Join the conversationon Twitter: @DevWeek // #DW2016 // #DevWeek Demos available on GitHub https://github.com/yorek/devweek2016