SlideShare a Scribd company logo
Introducing JSON
in MS SQL Server
Greg McMurray
Business Solutions Developer
Western Electricity Coordinating Council
Western Electricity Coordinating Council
• Non-profit to ensure the reliability of the western interconnection
• Approved Regional Entity by Federal Energy Regulatory Commission
• Create, monitor & enforce reliability standards
• Publish various models and independent perspective
• Covers 14 western states, 2 Canadian provinces and Baja Mexico
• We are hiring! https://www.wecc.biz/careers
Greg McMurray
• Worked in Aerospace, Branding & Marketing, Energy, Healthcare, Software
• Currently Business Solutions Developer atWECC
• Manager at Aritus Computer Services, L.L.C. for 18 years
• Active in many local user groups
• Find me online
• @goyuix
• https://www.linkedin.com/in/goyuix
• https://stackoverflow.com/cv/goyuix - top 3% of users
Hi! I am Greg – not Jason ↓
JSON? Who is Jason?
• “Discovered” by Douglas Crockford in 2001
• “JSON is XML for young people”
Scott Hanselman
DEVIntersection EU 2015
• Sample JSON:
{“User Group”:”Utah SQL Server Group”} ↑This is Scott - not Jason ↑
JavaScript Object Notation (JSON)
• What is JSON?
• Name-Value pairs
• Only 7 values: string, number, object, array, true, false, null
• Scalar values, objects and arrays - you can actually model a table or dataset quite nicely
• Number type: Supports both integer and floating point, but how they are implemented
is up to the parser.
• It is usually UTF-8 encoded – recommended to use NVARCHAR
Douglas Crockford – also not Jason↓
Want to Learn More About JSON?
• JSON.org is a great quick reference and starting place
• There are RFCs (4627 & 7159) and ECMA standards (404 & partly 262) as
published standards for data exchange
• Validation and schemas exist, not commonly used but are gaining in
popularity
Some JSON Gotchas & Quirks
• So what happens with dates or other complex types?
• Usually a convention of using one of the available types
• Date object might be represented by the ISO 8601 string
• How you do know if it was a string or date? Do you care?
• SQL Server JSON parser defaults to returning NVARCHAR typically
• JSON paths default to lax mode; strict mode changes behaviors
JSON Support in SQL Server
• Requires Compatibility Level 130 (SQL 2016)
• Some functions available at lower levels, but still require Server 2016 engine
• 120 might be the default even in new Azure SQL databases – you really need to check
• Available Functions
• ISJSON - more like IsValid JSON
• JSON_VALUE - for extracting scalar values
• JSON_MODIFY - like find / replace, but structured
• JSON_QUERY - useful for preventing SQL server from double escaping
• OPENJSON - treat JSON documents/objects/arrays as a table
• FOR JSON AUTO / PATH - convert query results to JSON document - similar to FOR XML
• Many functions can use / require a JSON path to identify data
• ProTip:You can use OPENROWSET to read saved JSON files
JSON Paths
• https://msdn.microsoft.com/en-us/library/mt577087.aspx
• String used in when calling various JSON functions
• Has a lax and strict mode; lax is the default
• Specify strict by including it at the start of your path, e.g. ‘strict $.Name’
• Strict mode changes:
• Generally raises errors on any possible parsing issues
• Error out on case-sensitivity issues rather than returning NULL
• Error out on missing keys for JSON_MODIFY, rather than adding them
• There is also an append mode used to add values to a JSON array
ISJSON
• https://msdn.microsoft.com/en-us/library/dn921896.aspx
• Returns 1 if the string contains valid JSON
• Examples:
• SELECT ISJSON('[1,2,3]')
• SELECT ISJSON('<tag>Not JSON</tag>')
• SELECT Id, Fragment FROM DocumentsWHERE ISJSON(Fragment) = 1
JSON_VALUE
• https://msdn.microsoft.com/en-us/library/dn921898.aspx
• Extracts a scalar value from a JSON string
• Examples:
• SELECT JSON_VALUE('{"Group":"SLC SQL UG"}', '$.Group')
• SELECT JSON_VALUE('{"Me":{"Name":"Greg"}}', '$.Me.Name')
JSON_QUERY
• https://msdn.microsoft.com/en-us/library/dn921884.aspx
• Extracts an object or an array from a JSON string
• JSON_VALUE returns scalar values; JSON_QUERY returns objects & arrays
• Examples:
• SELECT JSON_QUERY('{"a":[1,2,3]}', '$.a')
• SELECT JSON_QUERY('{"Me":{"Name":"Greg"}}', '$.Me')
JSON_MODIFY
• https://msdn.microsoft.com/en-us/library/dn921892.aspx
• Updates the value of a property in a JSON string and returns the new JSON
• Benefit: performs some validation and can safely replaces values
• Examples:
• SELECT JSON_MODIFY('{"Name":"Greg"}', '$.Name', 'Gregory')
• SELECT JSON_MODIFY('{"a":[1,2]}', 'append $.a', 3)
OPENJSON
• https://msdn.microsoft.com/en-us/library/dn921885.aspx
• Table-value function that parses JSON text and returns objects and properties as
rows and columns
• Requires COMPATIBILITY_LEVEL >= 130
• Supports defining a schema for JSON document
• Examples:
• SELECT * FROM OPENJSON('{"a":null,"b":1,"c":"2","d":[1,2],"e":{"f":6}}')
FOR JSON
• https://msdn.microsoft.com/en-us/library/ms173812.aspx
• Converts rows and columns to a JSON document
• Uses the FOR JSON syntax (similar to XML)
• Examples:
• SELECT name,object_id from sys.tables FOR JSON AUTO
Computed Columns
• Non-persisted data in the tables
• ProTip:You can build an index on a computed column
• This can be very useful for indexing into your JSON document
• Example:
ALTERTABLE [Table] ADD ColAS JSON_VALUE(JsonData, '$.Field')
Peeking Inside Pages –Table & Index
• Trace flags are your friend again: DBCCTRACEON(3604);
• DBCC IND('Database','dbo.Table',-1);
• DBCC PAGE('Database',FileNum,PagePID,3) WITHTABLERESULTS
• https://www.sqlskills.com/blogs/paul/inside-the-storage-engine-using-
dbcc-page-and-dbcc-ind-to-find-out-if-page-splits-ever-roll-back/
Want to learn more?
• BertWagner
https://blog.bertwagner.com/
• SQL Server 2016 support for JSON
https://msdn.microsoft.com/en-us/library/dn921897.aspx
• JSON in SQL Server (4 Parts)
https://blogs.technet.microsoft.com/dataplatforminsider/2016/01/05/json-in-sql-
server-2016-part-1-of-4/
• JSON on MSDN Channel 9
https://channel9.msdn.com/Search?term=JSON
Questions and Discussion
• Did I introduce everything you hoped to learn today?
• Would you like any clarifications on something we covered?
• Share with us: How are you going to apply this is your world?
• Feel free to reach out to me online:
• Twitter: @goyuix
• LinkedIn: https://www.linkedin.com/in/goyuix

More Related Content

PPT
Scala with MongoDB
PPTX
JSON Processing and mule
PPTX
Ajax xml json
KEY
Using NoSQL MongoDB with ColdFusion
PPTX
Mule json transformers
PDF
ITB2016 - NoSQL with mongodb and ColdFusion (CFML)
PPTX
Mule: JSON to Object
PPT
Tech Gupshup Meetup On MongoDB - 24/06/2016
Scala with MongoDB
JSON Processing and mule
Ajax xml json
Using NoSQL MongoDB with ColdFusion
Mule json transformers
ITB2016 - NoSQL with mongodb and ColdFusion (CFML)
Mule: JSON to Object
Tech Gupshup Meetup On MongoDB - 24/06/2016

What's hot (20)

PDF
SQL vs. NoSQL Databases
PPTX
Day 4 - Models
PDF
SDEC2011 NoSQL Data modelling
PDF
Cassandra
KEY
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
PPTX
Modeling JSON data for NoSQL document databases
PDF
Elastic Search
PPTX
Introduction to mongo db
PPTX
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
PDF
MongoDB for Coder Training (Coding Serbia 2013)
PDF
SQL vs NoSQL | MySQL vs MongoDB Tutorial | Edureka
PPTX
MongoDB basics & Introduction
PPT
Jsp + My Sql
PPTX
PostgreSQL - It's kind've a nifty database
PPTX
NoSQL Tel Aviv Meetup#1: NoSQL Data Modeling
PDF
Apache Any23 - Anything to Triples
PPT
Mysql grand
PPTX
Dataweave
SQL vs. NoSQL Databases
Day 4 - Models
SDEC2011 NoSQL Data modelling
Cassandra
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
Modeling JSON data for NoSQL document databases
Elastic Search
Introduction to mongo db
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
MongoDB for Coder Training (Coding Serbia 2013)
SQL vs NoSQL | MySQL vs MongoDB Tutorial | Edureka
MongoDB basics & Introduction
Jsp + My Sql
PostgreSQL - It's kind've a nifty database
NoSQL Tel Aviv Meetup#1: NoSQL Data Modeling
Apache Any23 - Anything to Triples
Mysql grand
Dataweave
Ad

Similar to Sql Server 2016 and JSON (20)

PDF
Native JSON Support in SQL2016
PPTX
Azure SQL & SQL Server 2016 JSON
PPTX
SQL Server 2016 JSON
PPTX
JSON-SQLServer2016.pptx dgsdgdsgdsgdsgsdgdsgdsg
PPTX
Demystifying JSON in SQL Server
PDF
JSON Support in DB2 for z/OS
PPTX
DBAs vs Developers: JSON in SQL Server - CBusPASS
PPTX
DBAs vs Developers - JSON in SQL Server
PPTX
JSON in SQL Server 2016
PPTX
DBAs vs Developers: JSON in SQL Server
PPTX
[Eng] Sql Saturday TorinoExpo - Sql Server 2016 JSON support
PPTX
JSON as a SQL Datatype
PDF
There is Javascript in my SQL
PDF
Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type
PPTX
BGOUG15: JSON support in MySQL 5.7
PPSX
Json in 18c and 19c
PDF
Json in Postgres - the Roadmap
 
PDF
How to Use JSON in MySQL Wrong
PPTX
MySQL Rises with JSON Support
PPTX
Power JSON with PostgreSQL
 
Native JSON Support in SQL2016
Azure SQL & SQL Server 2016 JSON
SQL Server 2016 JSON
JSON-SQLServer2016.pptx dgsdgdsgdsgdsgsdgdsgdsg
Demystifying JSON in SQL Server
JSON Support in DB2 for z/OS
DBAs vs Developers: JSON in SQL Server - CBusPASS
DBAs vs Developers - JSON in SQL Server
JSON in SQL Server 2016
DBAs vs Developers: JSON in SQL Server
[Eng] Sql Saturday TorinoExpo - Sql Server 2016 JSON support
JSON as a SQL Datatype
There is Javascript in my SQL
Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type
BGOUG15: JSON support in MySQL 5.7
Json in 18c and 19c
Json in Postgres - the Roadmap
 
How to Use JSON in MySQL Wrong
MySQL Rises with JSON Support
Power JSON with PostgreSQL
 
Ad

More from Greg McMurray (11)

PPTX
Power Platform Introduction - Utah PowerApps and Flow User Group
PPTX
SharePoint Search - August 2019 at Utah SharePoint User Group
PPTX
PowerShell Basics for Office Apps and Servers
PPTX
Introduction to SQL Server Graph DB
PPTX
Power BI Streaming Datasets - San Diego BI Users Group
PPTX
Dynamics 365 Web API - CRMUG April 2018
PPTX
SQL Server Temporal Tables
PPTX
Power BI Streaming Datasets
PPTX
Introduction to Microsoft Teams
PPTX
CRMUG Presentation on Dynamics CRM integration with SharePoint
PPTX
Real World Power Query for Excel and Power BI - SQL Saturday #576
Power Platform Introduction - Utah PowerApps and Flow User Group
SharePoint Search - August 2019 at Utah SharePoint User Group
PowerShell Basics for Office Apps and Servers
Introduction to SQL Server Graph DB
Power BI Streaming Datasets - San Diego BI Users Group
Dynamics 365 Web API - CRMUG April 2018
SQL Server Temporal Tables
Power BI Streaming Datasets
Introduction to Microsoft Teams
CRMUG Presentation on Dynamics CRM integration with SharePoint
Real World Power Query for Excel and Power BI - SQL Saturday #576

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Encapsulation theory and applications.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
KodekX | Application Modernization Development
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Programs and apps: productivity, graphics, security and other tools
Encapsulation theory and applications.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
KodekX | Application Modernization Development
Per capita expenditure prediction using model stacking based on satellite ima...
Network Security Unit 5.pdf for BCA BBA.
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
sap open course for s4hana steps from ECC to s4
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation_ Review paper, used for researhc scholars
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced methodologies resolving dimensionality complications for autism neur...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Digital-Transformation-Roadmap-for-Companies.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
20250228 LYD VKU AI Blended-Learning.pptx

Sql Server 2016 and JSON

  • 1. Introducing JSON in MS SQL Server Greg McMurray Business Solutions Developer Western Electricity Coordinating Council
  • 2. Western Electricity Coordinating Council • Non-profit to ensure the reliability of the western interconnection • Approved Regional Entity by Federal Energy Regulatory Commission • Create, monitor & enforce reliability standards • Publish various models and independent perspective • Covers 14 western states, 2 Canadian provinces and Baja Mexico • We are hiring! https://www.wecc.biz/careers
  • 3. Greg McMurray • Worked in Aerospace, Branding & Marketing, Energy, Healthcare, Software • Currently Business Solutions Developer atWECC • Manager at Aritus Computer Services, L.L.C. for 18 years • Active in many local user groups • Find me online • @goyuix • https://www.linkedin.com/in/goyuix • https://stackoverflow.com/cv/goyuix - top 3% of users Hi! I am Greg – not Jason ↓
  • 4. JSON? Who is Jason? • “Discovered” by Douglas Crockford in 2001 • “JSON is XML for young people” Scott Hanselman DEVIntersection EU 2015 • Sample JSON: {“User Group”:”Utah SQL Server Group”} ↑This is Scott - not Jason ↑
  • 5. JavaScript Object Notation (JSON) • What is JSON? • Name-Value pairs • Only 7 values: string, number, object, array, true, false, null • Scalar values, objects and arrays - you can actually model a table or dataset quite nicely • Number type: Supports both integer and floating point, but how they are implemented is up to the parser. • It is usually UTF-8 encoded – recommended to use NVARCHAR Douglas Crockford – also not Jason↓
  • 6. Want to Learn More About JSON? • JSON.org is a great quick reference and starting place • There are RFCs (4627 & 7159) and ECMA standards (404 & partly 262) as published standards for data exchange • Validation and schemas exist, not commonly used but are gaining in popularity
  • 7. Some JSON Gotchas & Quirks • So what happens with dates or other complex types? • Usually a convention of using one of the available types • Date object might be represented by the ISO 8601 string • How you do know if it was a string or date? Do you care? • SQL Server JSON parser defaults to returning NVARCHAR typically • JSON paths default to lax mode; strict mode changes behaviors
  • 8. JSON Support in SQL Server • Requires Compatibility Level 130 (SQL 2016) • Some functions available at lower levels, but still require Server 2016 engine • 120 might be the default even in new Azure SQL databases – you really need to check • Available Functions • ISJSON - more like IsValid JSON • JSON_VALUE - for extracting scalar values • JSON_MODIFY - like find / replace, but structured • JSON_QUERY - useful for preventing SQL server from double escaping • OPENJSON - treat JSON documents/objects/arrays as a table • FOR JSON AUTO / PATH - convert query results to JSON document - similar to FOR XML • Many functions can use / require a JSON path to identify data • ProTip:You can use OPENROWSET to read saved JSON files
  • 9. JSON Paths • https://msdn.microsoft.com/en-us/library/mt577087.aspx • String used in when calling various JSON functions • Has a lax and strict mode; lax is the default • Specify strict by including it at the start of your path, e.g. ‘strict $.Name’ • Strict mode changes: • Generally raises errors on any possible parsing issues • Error out on case-sensitivity issues rather than returning NULL • Error out on missing keys for JSON_MODIFY, rather than adding them • There is also an append mode used to add values to a JSON array
  • 10. ISJSON • https://msdn.microsoft.com/en-us/library/dn921896.aspx • Returns 1 if the string contains valid JSON • Examples: • SELECT ISJSON('[1,2,3]') • SELECT ISJSON('<tag>Not JSON</tag>') • SELECT Id, Fragment FROM DocumentsWHERE ISJSON(Fragment) = 1
  • 11. JSON_VALUE • https://msdn.microsoft.com/en-us/library/dn921898.aspx • Extracts a scalar value from a JSON string • Examples: • SELECT JSON_VALUE('{"Group":"SLC SQL UG"}', '$.Group') • SELECT JSON_VALUE('{"Me":{"Name":"Greg"}}', '$.Me.Name')
  • 12. JSON_QUERY • https://msdn.microsoft.com/en-us/library/dn921884.aspx • Extracts an object or an array from a JSON string • JSON_VALUE returns scalar values; JSON_QUERY returns objects & arrays • Examples: • SELECT JSON_QUERY('{"a":[1,2,3]}', '$.a') • SELECT JSON_QUERY('{"Me":{"Name":"Greg"}}', '$.Me')
  • 13. JSON_MODIFY • https://msdn.microsoft.com/en-us/library/dn921892.aspx • Updates the value of a property in a JSON string and returns the new JSON • Benefit: performs some validation and can safely replaces values • Examples: • SELECT JSON_MODIFY('{"Name":"Greg"}', '$.Name', 'Gregory') • SELECT JSON_MODIFY('{"a":[1,2]}', 'append $.a', 3)
  • 14. OPENJSON • https://msdn.microsoft.com/en-us/library/dn921885.aspx • Table-value function that parses JSON text and returns objects and properties as rows and columns • Requires COMPATIBILITY_LEVEL >= 130 • Supports defining a schema for JSON document • Examples: • SELECT * FROM OPENJSON('{"a":null,"b":1,"c":"2","d":[1,2],"e":{"f":6}}')
  • 15. FOR JSON • https://msdn.microsoft.com/en-us/library/ms173812.aspx • Converts rows and columns to a JSON document • Uses the FOR JSON syntax (similar to XML) • Examples: • SELECT name,object_id from sys.tables FOR JSON AUTO
  • 16. Computed Columns • Non-persisted data in the tables • ProTip:You can build an index on a computed column • This can be very useful for indexing into your JSON document • Example: ALTERTABLE [Table] ADD ColAS JSON_VALUE(JsonData, '$.Field')
  • 17. Peeking Inside Pages –Table & Index • Trace flags are your friend again: DBCCTRACEON(3604); • DBCC IND('Database','dbo.Table',-1); • DBCC PAGE('Database',FileNum,PagePID,3) WITHTABLERESULTS • https://www.sqlskills.com/blogs/paul/inside-the-storage-engine-using- dbcc-page-and-dbcc-ind-to-find-out-if-page-splits-ever-roll-back/
  • 18. Want to learn more? • BertWagner https://blog.bertwagner.com/ • SQL Server 2016 support for JSON https://msdn.microsoft.com/en-us/library/dn921897.aspx • JSON in SQL Server (4 Parts) https://blogs.technet.microsoft.com/dataplatforminsider/2016/01/05/json-in-sql- server-2016-part-1-of-4/ • JSON on MSDN Channel 9 https://channel9.msdn.com/Search?term=JSON
  • 19. Questions and Discussion • Did I introduce everything you hoped to learn today? • Would you like any clarifications on something we covered? • Share with us: How are you going to apply this is your world? • Feel free to reach out to me online: • Twitter: @goyuix • LinkedIn: https://www.linkedin.com/in/goyuix