SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Building REST API using
SQL Server and JSON Functions
Jovan Popovic
Microsoft Development Center Serbia
Program Manager
JSON
T-SQL language
Temporal
In-memory technologies
Intelligent Database.
Projects
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
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}
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
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)
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
[
{"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 […] {…}
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 […] {…}
Building REST API using
SQL Server
Exchanging JSON data with client applications
Built for Web Applications
Web Browser
Database
Web Server
</>
JSON
Initial request
AJAX
T-SQL
REST API
Get data from
SQL Database
Returning JSON results
1 Bike {….} […]
2 Helmet {….} […]
ORM
View
Model
{ }
{ }
Importing JSON objects
1 Bike {….} […]
2 Helmet {….} […]
ORMDTO
{ }
{ }
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 …”);
18
Lightweight and simple
Handles connection
Error handling
Asynchronous library
.BeginReaderAsync()
.WriteAsync()
Callbacks
Open source
DEMO
Questions?
Jovan Popovic
jovanpop@microsoft.com
@jovanpop_msft

Weitere ähnliche Inhalte

Kürzlich hochgeladen

Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 

Kürzlich hochgeladen (20)

Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 

Empfohlen

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Empfohlen (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Building REST API using SQL Server and JSON

  • 1. Building REST API using SQL Server and JSON Functions
  • 2. Jovan Popovic Microsoft Development Center Serbia Program Manager JSON T-SQL language Temporal In-memory technologies Intelligent Database. Projects
  • 3. 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 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
  • 14. REST API Get data from SQL Database
  • 15. Returning JSON results 1 Bike {….} […] 2 Helmet {….} […] ORM View Model { } { }
  • 16. Importing JSON objects 1 Bike {….} […] 2 Helmet {….} […] ORMDTO { } { }
  • 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 …”); 18 Lightweight and simple Handles connection Error handling Asynchronous library .BeginReaderAsync() .WriteAsync() Callbacks Open source
  • 19. DEMO