-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_JSON.sql
More file actions
154 lines (140 loc) · 4.67 KB
/
11_JSON.sql
File metadata and controls
154 lines (140 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**************************************************************
* SQL Server 2022 JSON Data Tutorial
* Description: This script demonstrates working with JSON data
* in SQL Server. It covers table creation with JSON
* columns, inserting and validating JSON, constructing
* JSON arrays and objects, modifying JSON values,
* testing JSON paths, and extracting JSON data using
* built-in JSON functions.
**************************************************************/
-------------------------------------------------
-- Region: 0. Initialization
-------------------------------------------------
/*
Ensure you are using the target database.
*/
USE TestDB;
GO
-------------------------------------------------
-- Region: 1. Creating Table with JSON Data
-------------------------------------------------
/*
1.1 Create a table to store orders with a JSON column.
*/
IF OBJECT_ID(N'dbo.Orders', N'U') IS NOT NULL
DROP TABLE dbo.Orders;
GO
CREATE TABLE dbo.Orders
(
OrderID INT PRIMARY KEY,
OrderDetails NVARCHAR(MAX) -- Stores JSON data
);
GO
-------------------------------------------------
-- Region: 2. Inserting Sample JSON Data
-------------------------------------------------
/*
2.1 Insert sample records containing JSON-formatted order details.
*/
INSERT INTO dbo.Orders (OrderID, OrderDetails)
VALUES
(1, N'{"Customer":"John Doe","Items":[{"Product":"Laptop","Quantity":1},{"Product":"Mouse","Quantity":2}]}'),
(2, N'{"Customer":"Jane Smith","Items":[{"Product":"Tablet","Quantity":1},{"Product":"Keyboard","Quantity":1}]}');
GO
-------------------------------------------------
-- Region: 3. Validating JSON Data
-------------------------------------------------
/*
3.1 Test whether the string in OrderDetails is valid JSON.
*/
SELECT
OrderID,
ISJSON(OrderDetails) AS IsValidJSON
FROM dbo.Orders;
GO
-------------------------------------------------
-- Region: 4. Constructing JSON Arrays
-------------------------------------------------
/*
4.1 Construct a JSON array from explicit expressions.
*/
SELECT JSON_ARRAY('Laptop', 'Mouse', 'Keyboard') AS ProductsArray;
GO
/*
4.2 Construct a JSON array from an aggregation of SQL data.
*/
SELECT JSON_ARRAYAGG(ProductName) AS ProductsArray
FROM (VALUES ('Laptop'), ('Mouse'), ('Keyboard')) AS Products(ProductName);
GO
-------------------------------------------------
-- Region: 5. Modifying JSON Data
-------------------------------------------------
/*
5.1 Update the value of a property in a JSON string.
This example updates the Customer name in OrderDetails for OrderID = 1.
*/
UPDATE dbo.Orders
SET OrderDetails = JSON_MODIFY(OrderDetails, '$.Customer', 'John Smith')
WHERE OrderID = 1;
GO
-------------------------------------------------
-- Region: 6. Constructing JSON Objects
-------------------------------------------------
/*
6.1 Construct a JSON object from explicit expressions.
*/
SELECT JSON_OBJECT('Customer' VALUE 'John Doe', 'OrderID' VALUE 1) AS OrderObject;
GO
-------------------------------------------------
-- Region: 7. Testing JSON Paths
-------------------------------------------------
/*
7.1 Test whether a specified SQL/JSON path exists in the input JSON string.
*/
SELECT
OrderID,
JSON_PATH_EXISTS(OrderDetails, '$.Items[0].Product') AS PathExists
FROM dbo.Orders;
GO
-------------------------------------------------
-- Region: 8. Extracting Data from JSON
-------------------------------------------------
/*
8.1 Extract an object or array from a JSON string.
This example extracts the Items array.
*/
SELECT
OrderID,
JSON_QUERY(OrderDetails, '$.Items') AS ItemsArray
FROM dbo.Orders;
GO
/*
8.2 Extract a scalar value from a JSON string.
This example extracts the Customer name.
*/
SELECT
OrderID,
JSON_VALUE(OrderDetails, '$.Customer') AS CustomerName
FROM dbo.Orders;
GO
-------------------------------------------------
-- Region: 9. Combining Multiple JSON Functions
-------------------------------------------------
/*
9.1 Combine multiple JSON functions to validate, test path, construct
JSON object, and build a JSON array of specific properties.
*/
SELECT
OrderID,
ISJSON(OrderDetails) AS IsValidJSON,
JSON_PATH_EXISTS(OrderDetails, '$.Items[0].Product') AS PathExists,
JSON_OBJECT('OrderID' VALUE OrderID, 'Customer' VALUE JSON_VALUE(OrderDetails, '$.Customer')) AS OrderObject,
JSON_ARRAY(
JSON_VALUE(OrderDetails, '$.Items[0].Product'),
JSON_VALUE(OrderDetails, '$.Items[1].Product')
) AS ProductsArray
FROM dbo.Orders;
GO
-------------------------------------------------
-- Region: End of Script
-------------------------------------------------