Download presentation
Presentation is loading. Please wait.
Published byBerenice McKenzie Modified over 6 years ago
1
Building REST API using SQL Server and JSON Functions
2
Jovan Popovic Program Manager Projects
Microsoft Development Center Serbia Program Manager JSON T-SQL language Temporal In-memory technologies Intelligent Database. Projects
3
JSON Support in SQL Server Building REST API using SQL Server
Agenda JSON Support in SQL Server Building REST API using SQL Server
4
JSON Support in SQL Server
Combining relational and semi-structured data
5
JSON in SQL Database
6
Combine relational and JSON data
ID Name Type Price Tags Data 1 Bike 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 299.99 [“sales”] {“Tyres”:[“300c”,”35C”],Weight”:9.5,”Gender”:”M”} 9 {“Doors”:2, “Seats”:2, “MPG”:35} 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)
7
Optimizing JSON Queries
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')
8
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') 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
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
SQL results as JSON 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 [ {"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": ,"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}} ]
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": ,"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}} ] ID Name Price Tags Data 15 Bike 100 […] {…} 16 Car 29000 17 BB Ba… 29,99 18 Blade 18.50 19 Helmet 41.99 SELECT * FROM OPENJSON WITH ( ProductID int, Name nvarchar(50), Price money, Type nvarchar(max) AS JSON, Data nvarchar(max) AS JSON)
12
Building REST API using SQL Server
Exchanging JSON data with client applications
13
Built for Web Applications
Web Browser Web Server Initial request </> Database AJAX T-SQL JSON
14
REST API REST End-point Get data from SQL Database
15
Returning JSON results
Id Title Data Tags 1 Bike {….} […] 2 Helmet { } View Model ORM { }
16
Importing JSON objects
Id Title Data Tags 1 Bike {….} […] 2 Helmet { } DTO ORM { }
17
SQL Server Samples on GitHub
Product Catalog REST API
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 …”); Lightweight and simple Handles connection Error handling Asynchronous library .BeginReaderAsync() .WriteAsync() Callbacks Open source
19
DEMO
20
Questions? Jovan Popovic @jovanpop_msft
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.