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
2. Jovan Popovic
Microsoft Development Center Serbia
Program Manager
JSON
T-SQL language
Temporal
In-memory technologies
Intelligent Database.
Projects
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. 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. 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. 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
[
{"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. 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. Building REST API using
SQL Server
Exchanging JSON data with client applications
13. Built for Web Applications
Web Browser
Database
Web Server
</>
JSON
Initial request
AJAX
T-SQL
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