Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Building REST API using SQL Server and JSON

2,124 views

Published on

SQL Server 2016 and Azure SQL Database provide built-in functions that enable you to easily transform JSON documents into relational format and vice versa. Exchanging JSON documents between client applications and SQL Database is now easier than ever. In this session you will see how to create REST API using built-in JSON functionalities in SQL Server 2016 and Azure SQL Database.
Video is available here: https://www.youtube.com/watch?v=0m6GXF3-5WI

Published in: Software
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Building REST API using SQL Server and JSON

  1. 1. Building REST API using SQL Server and JSON Functions
  2. 2. Jovan Popovic Microsoft Development Center Serbia Program Manager JSON T-SQL language Temporal In-memory technologies Intelligent Database. Projects
  3. 3. Agenda JSON Support in SQL Server Building REST API using SQL Server
  4. 4. JSON Support in SQL Server Combining relational and semi-structured data
  5. 5. JSON in SQL Database
  6. 6. Combine relational and JSON data SELECT ProductID, Name, Tags, JSON_VALUE(Data, '$.Weight') FROM Product WHERE Type=1 AND JSON_VALUE(Data, '$.Gender') = 'F' ORDER BY Price, CAST(JSON_VALUE(Data, '$.Weight') as float) ID Name Type Price Tags Data 1 Bike 1 399.99 [“promo”] {“Gears”:20, “Weight”:9.5,”Gender”:”M”} 2 Helmet 7 120.99 [“promo”,”sales”] {“Visor”:true, “WxDxH”: [32,26.4,22]} 3 Car 6 29,500 {“Doors”:4, “Seats”:5, “MPG”:29} 8 Bike 1 299.99 [“sales”] {“Tyres”:[“300c”,”35C”],Weight”:9.5,”Gender”:”M”} 9 Car 6 29,500 [“promo”] {“Doors”:2, “Seats”:2, “MPG”:35}
  7. 7. select JSON_VALUE(Data, '$.Type') as Type, Color, AVG( cast(JSON_VALUE(Data, '$.ManufacturingCost') as float) ) as Cost from Product group by JSON_VALUE(Data, '$.Type'), Color having JSON_VALUE(Data, '$.Type') is not null order by JSON_VALUE(Data, '$.Type') Optimizing JSON Queries
  8. 8. select JSON_VALUE(Data, '$.Type') as Type, Color, AVG( cast(JSON_VALUE(Data, '$.ManufacturingCost') as float) ) as Cost from Product group by JSON_VALUE(Data, '$.Type'), Color having JSON_VALUE(Data, '$.Type') is not null order by JSON_VALUE(Data, '$.Type') Index on JSON Data alter table product add Type AS JSON_VALUE(Data, '$.Type'), ManufacturingCost AS cast(JSON_VALUE(Data, '$.ManufacturingCost') as float) create index json_index on Product(Type, Color) include (ManufacturingCost)
  9. 9. Index on JSON Data select JSON_VALUE(Data, '$.Type') as Type, Color, AVG( cast(JSON_VALUE(Data, '$.ManufacturingCost') as float) ) as Cost from Product group by JSON_VALUE(Data, '$.Type'), Color having JSON_VALUE(Data, '$.Type') is not null order by JSON_VALUE(Data, '$.Type')
  10. 10. SQL results as JSON [ {"ProductID":15,"Name":“Bike","Price":100,"Data":{"Type":"Part","MadeIn":“SRB"}}, {"ProductID":16,"Name":“Car","Price":29000,"Tags":["promo"],"Data":{"Cost":11.67,"Type":"Part"}}, {"ProductID":17,"Name":"BB Ball Bearing","Price":28.99,"Data":{"Cost":21.162700,"Type":"Part"}}, {"ProductID":18,"Name":"Blade","Price":18.50,"Tags":["new"],"Data":{}}, {"ProductID":19,"Name":"Helmet","Price":41.99,"Tags":["promo"],"Data":{"Cost":30.65}} ] SELECT ProductID, Name, Price, Tags = JSON_QUERY(Tags), Data = JSON_QUERY(Data) FROM Product FOR JSON PATH ID Name Price Tags Data 15 Bike 100 […] {…} 16 Car 29000 […] {…} 17 BB Ba… 29,99 {…} 18 Blade 18.50 […] {…} 19 Helmet 41.99 […] {…}
  11. 11. JSON to result set [ {"ProductID":15,"Name":“Bike","Price":100,"Data":{"Type":"Part","MadeIn":“SRB"}}, {"ProductID":16,"Name":“Car","Price":29000,"Tags":["promo"],"Data":{"Cost":11.67,"Type":"Part"}}, {"ProductID":17,"Name":"BB Ball Bearing","Price":28.99,"Data":{"Cost":21.162700,"Type":"Part"}}, {"ProductID":18,"Name":"Blade","Price":18.50,"Tags":["new"],"Data":{}}, {"ProductID":19,"Name":"Helmet","Price":41.99,"Tags":["promo"],"Data":{"Cost":30.65}} ] SELECT * FROM OPENJSON (@p) WITH ( ProductID int, Name nvarchar(50), Price money, Type nvarchar(max) AS JSON, Data nvarchar(max) AS JSON) ID Name Price Tags Data 15 Bike 100 […] {…} 16 Car 29000 […] {…} 17 BB Ba… 29,99 {…} 18 Blade 18.50 […] {…} 19 Helmet 41.99 […] {…}
  12. 12. Building REST API using SQL Server Exchanging JSON data with client applications
  13. 13. Built for Web Applications Web Browser Database Web Server </> JSON Initial request AJAX T-SQL
  14. 14. REST API Get data from SQL Database
  15. 15. Returning JSON results 1 Bike {….} […] 2 Helmet {….} […] ORM View Model { } { }
  16. 16. Importing JSON objects 1 Bike {….} […] 2 Helmet {….} […] ORMDTO { } { }
  17. 17. SQL Server Samples on GitHub Product Catalog REST API
  18. 18. Data Access Component var pipe = new QueryPipe(“connection string”); await pipe.Stream(“SELECT …. FOR JSON PATH”, Response.Body); var mapper = new QueryMapper(“connection string”); var json = await mapper.GetStringAsync(“SELECT …. FOR JSON PATH”); var cmd = new Command(“connection string”); await cmd.ExecuteNonQuery(“UPDATE …”); 18 Lightweight and simple Handles connection Error handling Asynchronous library .BeginReaderAsync() .WriteAsync() Callbacks Open source
  19. 19. DEMO
  20. 20. Questions? Jovan Popovic jovanpop@microsoft.com @jovanpop_msft

×