Building REST API using SQL Server and JSON Functions
Jovan Popovic Program Manager Projects Microsoft Development Center Serbia Program Manager JSON T-SQL language Temporal In-memory technologies Intelligent Database. Projects
JSON Support in SQL Server Building REST API using SQL Server Agenda JSON Support in SQL Server Building REST API using SQL Server
JSON Support in SQL Server Combining relational and semi-structured data
JSON in SQL Database
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)
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')
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)
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')
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":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}} ]
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}} ] 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 (@p) WITH ( ProductID int, Name nvarchar(50), Price money, Type nvarchar(max) AS JSON, Data nvarchar(max) AS JSON)
Building REST API using SQL Server Exchanging JSON data with client applications
Built for Web Applications Web Browser Web Server Initial request </> Database AJAX T-SQL JSON
REST API REST End-point Get data from SQL Database
Returning JSON results Id Title Data Tags 1 Bike {….} […] 2 Helmet { } View Model ORM { }
Importing JSON objects Id Title Data Tags 1 Bike {….} […] 2 Helmet { } DTO ORM { }
SQL Server Samples on GitHub Product Catalog REST API
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
DEMO
Questions? Jovan Popovic jovanpop@microsoft.com @jovanpop_msft