Here's another option if you have json data stored in a column in multiple records and you want to parse that json for each record.
Cross apply to pass that data column containing the json data to OPENJSON and then you can use WITH.
This is assuming the json data in all the records has the same structure.
You would need to obviously adjust for your data needs. This example is json with an array called "JsonData".
DECLARE @JsonData TABLE
(
[ID] INT
, [data] NVARCHAR(MAX)
);
--Load Test data
INSERT INTO @JsonData (
[ID]
, [data]
)
VALUES ( 1 -- ID - int
, N'{ "JsonData":[
{ "Column1":"1","Column2":"TestData1","Column3":"This is more test data for number 1"}
,{ "Column1":"2","Column2":"TestData2","Column3":"This is more test data for number 2"}
,{ "Column1":"3","Column2":"TestData3","Column3":"This is more test data for number 3"}
] }' -- data - nvarchar(max)
)
, ( 2 -- ID - int
, N'{ "JsonData":[
{ "Column1":"4","Column2":"TestData4","Column3":"This is more test data for number 4"}
,{ "Column1":"5","Column2":"TestData5","Column3":"This is more test data for number 5"}
,{ "Column1":"6","Column2":"TestData6","Column3":"This is more test data for number 6"}
] }' -- data - nvarchar(max)
)
, ( 3 -- ID - int
, N'{ "JsonData":[
{ "Column1":"7","Column2":"TestData7","Column3":"This is more test data for number 7"}
,{ "Column1":"8","Column2":"TestData8","Column3":"This is more test data for number 8"}
,{ "Column1":"9","Column2":"TestData9","Column3":"This is more test data for number 9"}
] }' -- data - nvarchar(max)
);
--Here you can see your scenario of json in multiple recrods.
SELECT *
FROM @JsonData;
--We can parse that json for each record with a cross apply and using WITH
SELECT [a].[ID]
, [b].[Column1]
, [b].[Column2]
, [b].[Column3]
FROM @JsonData [a]
CROSS APPLY
OPENJSON([a].[data], '$.JsonData')
WITH (
[Column1] INT '$.Column1'
, [Column2] NVARCHAR(200) '$.Column2'
, [Column3] NVARCHAR(200) '$.Column3'
) [b];
If your json has nested arrays, that can still be accomplished, basically another cross apply to get into the nested array. Here's an example of dealing with a nested array in this scenario:
DECLARE @JsonData TABLE
(
[ID] INT
, [data] NVARCHAR(MAX)
);
--Load Test data
INSERT INTO @JsonData (
[ID]
, [data]
)
VALUES ( 1 -- ID - int
, N'{
"JsonData": [
{
"Column1": "1",
"Column2": "TestData1",
"Column3": "This is more test data for number 1",
"JsonDataNested": [
{
"NestedColumn1": "NestedColumn1",
"NestedColumn2": "NestedColumn2"
}
,{
"NestedColumn1": "NestedColumn1_1",
"NestedColumn2": "NestedColumn2_2"
}
]
}
]
}' -- data - nvarchar(max)
);
--We can parse that json for each record with a cross apply and using WITH
SELECT [a].[ID]
, [b].[Column1]
, [b].[Column2]
, [b].[Column3]
, [c].*
FROM @JsonData [a]
CROSS APPLY
OPENJSON([a].[data], '$.JsonData') --top array
WITH (
[Column1] INT '$.Column1'
, [Column2] NVARCHAR(200) '$.Column2'
, [Column3] NVARCHAR(200) '$.Column3'
, [JsonDataNested] NVARCHAR(MAX) AS JSON --Nested array
) [b]
CROSS APPLY
OPENJSON([b].[JsonDataNested], '$')
WITH (
[NestedColumn1] NVARCHAR(200) '$.NestedColumn1'
, [NestedColumn2] NVARCHAR(200) '$.NestedColumn2'
) [c];