Meet JSON In SQL Server 2016 Russ Loski Preparations: Have Visual Studio open with a console project created with JSON file. Clear the existing file. Open the JSON sample and have the SQL files displaying. Russ Loski
Gold Sponsor Silver Sponsor Bronze Sponsor Swag Sponsor SQL Saturday Host 2 | Blog Sponsor San Antonio SQL User Group Personal Sponsor Thomas LeBlanc aka The Smiling DBA Naomi Williams aka Naomi The SQL DBA The user groups meets every 3rd Wednesday from 6pm-8pm @ New Horizons Training Center on 2727 NW Loop 410 #103, San Antonio, TX 78230 More info? Jim.Steiner@Rackspace.com
Russ Loski SQL Server ETL developer from the Dallas Fort Worth area Member of the North Texas SQL Server Users Group Curious about structured data Regular speaker at SQL Saturdays Grand dad for active 5 year old RussLoski@SQLMovers.com www.SQLMovers.com @sqlmovers https://www.linkedin.com/in/russloski
Structured Or Not Structured Structure from none to RDMS
Structured Or Not Structured <?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01 </publish_date> <description>An in-depth look at creating applications with XML.</description> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> </book> https://msdn.microsoft.com/en-us/library/ms762271(v=vs.85).aspx
Structured Or Not Structured policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity 119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1 448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3 206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1 333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3 172534,FL,CLAY COUNTY,0,254281.5,0,254281.5,254281.5,246144.49,0,0,0,0,30.060614,-81.702675,Residential,Wood,1 https://support.spatialkey.com/spatialkey-sample-csv-data/
Structured Or Not Structured ISA*00* *00* *12*ABCCOM *01*999999999 *101127*1719*U*00400*000003438*0*P*> GS*PO*4405197800*999999999*20101127*1719*1421*X*004010VICS ST*834*0179 BGN*00*1*20050315*110650****4 REF*38*SAMPLE_POLICY_NUMBER DTP*303*D8*20080321 N1*P5*COMPAN_NAME*FI*000000000 INS*Y*18*030*20*A REF*0F*SUBSCRIBER_NUMBER NM1*IL*1*JOHN DOE*R***34*1*0000000 PER*IP**HP*2138051111 N3*123 SAMPLE RD N4*CITY*ST*12345 DMG*D8*19690101 *F HD*030 DTP*348*D8*20080101 REF*1L*INDIV_POLICY_NO SE*16*0179 GE*1*1421 IEA*1*000003438 http://www.1edisource.com/transaction-sets?tset=834
Structured Or Not Structured Structure from none to RDMS
Structured Or Not Structured Images Printed Books Book on line Web Page Web Page w/ Tables Comma Delimited Structure from none to RDMS SQL Server provides means for working with XML. It even will let you work with comma delimited files. What SQL Server 2016 is the ability to work with JSON. Also, SQL Server 2016 can parse comma delimited variables strings and column strings, but that is for another day. JSON XML { "name"="Russ", "location"="Tyler" } OLTP DB with RDMS Pure Relational model
Microsoft Connect What is JSON?
Agenda What is JSON and why is it important Reading JSON in SQL Server Writing JSON in SQL Server
What is JSON JavaScript Object Notation Lightweight data format akin to XML Self-describing Converts directly to JavaScript objects Easy to work with in JavaScript than other formats Provide definition What is JSON?
Simple JSON {"employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"} ]} This examples comes from http://www.w3schools.com/json/default.asp What is JSON?
JSON Examples – Data Types Numbers: 1, 2, 3.4 – No quotes Strings: "test" , "1", "true" – Double quotes Boolean: true, false – No quotes Array: [ , ] – Square brackets surrounding comma delimited list Object: {"name1":"value1", "name2":"value2"} Null: null – This indicates empty item. http://www.tutorialspoint.com/json/json_data_types.htm What is JSON?
JSON Examples - Arrays [ 1 , 2, 3, 4, 5] [ "Russ" , "Gail" , "Don" , "Julie"] [ {"phoneType" : "cell" , "number" : "2145551111" } , {"phoneType" : "home", "number" : "9725551111" } ] [ 1, "Russ", { "address1" : "444 Main", "city" : "Tyler" } ] You can have arrays of any type. And the array does need to have items of the same type. What is JSON?
Why JSON? Agile. No structure up front Light. Not the tag structure My focus in this slide is not to argue for or against JSON. It is simply to repeat some of the arguments for JSON.
Reading JSON in SQL Server 2016 Reading JSON into row set Extracting single value from JSON Identifying JSON Reading JSON
JSON Data Type? No JSON data type Use string (varchar, nvarchar up to max) Richer than XML support in early 2000s Reading JSON
Reading JSON into Row Set OPENJSON ( NVARCHAR Column/Variable/Constant , JSON Path Expression ) Reading JSON
OpenJSON Examples SELECT * FROM OPENJSON (@var, N'$') SELECT * FROM tbl OUTER APPLY OPENJSON(tbl.Col, N'$.property') v SELECT * FROM tbl OUTER APPLY OPENJSON(tbl.Col, N'$.property) WITH ( Prop1 VARCHAR(20) , Prop2 INT N'$.OtherProperty' , Prop3 date N'strict $.Prop3' ) as v Reading JSON
Reading value from JSON JSON_Value ( NVARCHAR Column/Variable/Constant , JSON Path Expression ) Reading JSON
Reading JSON document from JSON JSON_Query ( NVARCHAR Column/Variable/Constant , JSON Path Expression ) Reading JSON
JSON_Value/JSON_Query Examples SELECT JSON_Value (@var, N'$.property') SELECT JSON_Value( tbl.Col, N'$.property') FROM tbl SELECT JSON_Query (@var, N'$.property') SELECT JSON_Query( tbl.Col, N'$.property') Reading JSON
Demo Reading JSON Reading JSON
Writing JSON FOR JSON AUTO/PATH Options: Writing JSON INCLUDE_NULL_VALUES WITHOUT_ARRAY_WRAPPER ROOT('root') https://msdn.microsoft.com/en-us/library/dn921882.aspx Writing JSON
Demo FOR JSON Writing JSON The query that I use displays sales orders from the Adventureworks database. I deliberately sort the output in order to demonstrate that order matters for FOR JSON AUTO I think that I will have the entire query formatted almost the way that I want. Change the table name aliases using the design tool. The order of the tables in the join matters for how the JSON is formatted. Writing JSON
Use Cases for JSON One-off queries Analysis of JSON data ETL. Loading JSON data into structured database Closing?
References https://msdn.microsoft.com/en-US/library/dn921897.aspx http://www.sqlpass.org/24hours/2016/edp/Sessions/Details.aspx?sid=49113 http://www.sqlservercentral.com/articles/JSON/141175/ https://dataonwheels.wordpress.com/2016/04/20/json-in-sql-server-2016- the-good-the-bad-and-the-ugly/