英文:
Get JSON element using SQL without specifying record index?
问题
以下是翻译好的代码部分:
SELECT
DocumentFormStatus = JSON_VALUE(LWDocs.DMDetails, '$[0].DocumentFormStatus'),
DocumentType = JSON_VALUE(LWDocs.DMDetails, '$[0].DocumentType')
FROM OPENROWSET(
'CosmosDB', N'account=...;database=...;region=...;key=...',cDB
) AS LWDocs
英文:
We have JSON data in this format:
[{"DocumentFormStatus":"Submitted","DocumentType":"DocType1"},{"DocumentFormStatus":"Submitted","DocumentType":"DocType2"},{"DocumentFormStatus":"Submitted","DocumentType":"DocType3"},{"DocumentFormStatus":"Submitted","DocumentType":"DocType4"}]
I would like to be able to get the data in tabular format like so:
DocumentFormStatus | DocumentType |
---|---|
Submitted | DocType1 |
Submitted | DocType2 |
Submitted | DocType3 |
Submitted | DocType4 |
I am able to pull the data like below however, this is obviously limited to the element index specified (I have to run multiple instances of this query and do a UNION between them all). How can I get all records in a JSON element without specifying the record index?
SELECT
DocumentSubmissionStatus = JSON_VALUE(LWDocs.DMDetails, '$[0].DocumentFormStatus'),
DocumentType = JSON_VALUE(LWDocs.DMDetails, '$[0].DocumentType')
FROM OPENROWSET(
'CosmosDB', N'account=...;database=...;region=...;key=...',cDB
) AS LWDocs
答案1
得分: 1
你可以使用OPENJSON
declare @Json nvarchar(max)='
[{"DocumentFormStatus":"Submitted","DocumentType":"DocType1"}
,{"DocumentFormStatus":"Submitted","DocumentType":"DocType2"}
,{"DocumentFormStatus":"Submitted","DocumentType":"DocType3"}
,{"DocumentFormStatus":"Submitted","DocumentType":"DocType4"}]
'
SELECT
DocumentFormStatus,DocumentType
FROM
OPENJSON(@json)
WITH
(
DocumentFormStatus varchar(100) N'$.DocumentFormStatus'
,DocumentType varchar(100) N'$.DocumentType'
) AS a
英文:
you can use OPENJSON
declare @Json nvarchar(max)='
[{"DocumentFormStatus":"Submitted","DocumentType":"DocType1"}
,{"DocumentFormStatus":"Submitted","DocumentType":"DocType2"}
,{"DocumentFormStatus":"Submitted","DocumentType":"DocType3"}
,{"DocumentFormStatus":"Submitted","DocumentType":"DocType4"}]
'
SELECT
DocumentFormStatus,DocumentType
FROM
OPENJSON(@json)
WITH
(
DocumentFormStatus varchar(100) N'$.DocumentFormStatus'
,DocumentType varchar(100) N'$.DocumentType'
) AS a
答案2
得分: 1
你的JSON数据似乎是在一个表格中。使用CROSS APPLY。
示例
Declare @Yourtable table (id int, DMDetails varchar(max))
Insert Into @Yourtable values
(1, '[{"DocumentFormStatus":"Submitted","DocumentType":"DocType1"},{"DocumentFormStatus":"Submitted","DocumentType":"DocType2"},{"DocumentFormStatus":"Submitted","DocumentType":"DocType3"},{"DocumentFormStatus":"Submitted","DocumentType":"DocType4"}]')
Select A.ID
,B.*
From @Yourtable A
Cross Apply OpenJSON(DMDetails)
with ( DocumentFormStatus varchar(150)
,DocumentType varchar(150)
) B
结果
ID DocumentFormStatus DocumentType
1 Submitted DocType1
1 Submitted DocType2
1 Submitted DocType3
1 Submitted DocType4
英文:
It seems your JSON is in a table. Use a CROSS APPLY
Example
Declare @Yourtable table (id int,DMDetails varchar(max))
Insert Into @Yourtable values
(1, '[{"DocumentFormStatus":"Submitted","DocumentType":"DocType1"},{"DocumentFormStatus":"Submitted","DocumentType":"DocType2"},{"DocumentFormStatus":"Submitted","DocumentType":"DocType3"},{"DocumentFormStatus":"Submitted","DocumentType":"DocType4"}]')
Select A.ID
,B.*
From @Yourtable A
Cross Apply OpenJSON(DMDetails)
with ( DocumentFormStatus varchar(150)
,DocumentType varchar(150)
) B
Results
ID DocumentFormStatus DocumentType
1 Submitted DocType1
1 Submitted DocType2
1 Submitted DocType3
1 Submitted DocType4
答案3
得分: 1
这是另一种使用 JSON_VALUE
和 OPENJSON
来执行的方法:
SELECT JSON_VALUE ( j.value , '$.DocumentFormStatus' ) as DocumentFormStatus,
JSON_VALUE ( j.value , '$.DocumentType' ) as DocumentType
FROM LWDocs
CROSS APPLY OPENJSON(LWDocs.DMDetails) as j
英文:
This is an other way to do it using JSON_VALUE
and OPENJSON
SELECT JSON_VALUE ( j.value , '$.DocumentFormStatus' ) as DocumentFormStatus,
JSON_VALUE ( j.value , '$.DocumentType' ) as DocumentType
FROM LWDocs
CROSS APPLY OPENJSON(LWDocs.DMDetails) as j
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论