使用SQL获取JSON元素而不指定记录索引?

huangapple go评论63阅读模式
英文:

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_VALUEOPENJSON 来执行的方法:

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

Demo here

huangapple
  • 本文由 发表于 2023年4月19日 19:03:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定