简体   繁体   English

从嵌套数组中提取多列数据

[英]Extract data from nested array for multiple columns

I am using SQL Server 2016 and want to extract data from array.我正在使用 SQL Server 2016 并想从数组中提取数据。

But I got stuck.但我被卡住了。

DECLARE @idvalue NVARCHAR(MAX)='[{"testId":"b29b2327-527c-456d-8346-6bd22d198f21","testValue":"FAILURE","test":"b29b2327-527c-456d-8346-6bd22d198f21:FAILURE","testStartTimestamp":"2020-06-25T09:22:17.299Z"}
,{"testId":"4674bc9c-9551-496b-b488-8e138a4dc459","testValue":"FAILURE","test":"4674bc9c-9551-496b-b488-8e138a4dc459:FAILURE","testStartTimestamp":"2020-06-25T09:22:17.299Z"},{"testId":"38c20ac5-dbb7-43ad-b139-f8fde13d0ea5","testValue":"FAILURE","test":"38c20ac5-dbb7-43ad-b139-f8fde13d0ea5:FAILURE","testStartTimestamp":"2020-06-25T09:22:17.3Z"},{"testId":"4a3b3102-d3fa-4c2c-b3cf-46e6822fae5c","testValue":"FAILURE","test":"4a3b3102-d3fa-4c2c-b3cf-46e6822fae5c:FAILURE","testStartTimestamp":"2020-06-25T09:22:17.3Z"},{"testId":"f80bec6d-ab5c-4f63-8aea-cd2f5179195e","testValue":"FAILURE","test":"f80bec6d-ab5c-4f63-8aea-cd2f5179195e:FAILURE","testStartTimestamp":"2020-06-25T09:22:17.3Z"},{"testId":"e75896d2-f314-4423-87be-ea70b2ba5adb","testValue":"FAILURE","test":"e75896d2-f314-4423-87be-ea70b2ba5adb:FAILURE","testStartTimestamp":"2020-06-25T09:22:17.301Z"}]';

select P_conversations_conversationId,
       P_conversations_participants_sessions_flow_outcomes,
       JSON_VALUE(REPLACE(P_conversations_participants_sessions_flow_outcomes,'[',''),'$.testId') as testId,
       JSON_VALUE(REPLACE(REPLACE(P_conversations_participants_sessions_flow_outcomes,'[',''),']',''),'$.testEndTimestamp') as testEndTimestamp,
       JSON_VALUE(REPLACE(@idvalue,'[',''),'$.testValue') AS [testValue],
       JSON_VALUE(REPLACE(@idvalue,'[',''),'$.testStartTimestamp') as testStartTimestamp,
       JSON_VALUE(REPLACE(@idvalue,'[',''),'$.test') as test 
FROM Dashboardtable;

In the declare statement I have given sample data.declare语句中,我给出了示例数据。

Yoy may try to parse the input JSON with OPENJSON() and explicit schema: Yoy 可能会尝试使用OPENJSON()和显式模式解析输入 JSON:

 SELECT *
 FROM OPENJSON(@idvalue) WITH (
    testId varchar(36) '$.testId',
    testValue varchar(100) '$.testValue',
    test varchar(100) '$.test',
    testStartTimestamp datetime2(3) '$.testStartTimestamp'
 )

Result:结果:

testId                                  testValue   test                                             testStartTimestamp
b29b2327-527c-456d-8346-6bd22d198f21    FAILURE     b29b2327-527c-456d-8346-6bd22d198f21:FAILURE    2020-06-25 09:22:17.299
4674bc9c-9551-496b-b488-8e138a4dc459    FAILURE     4674bc9c-9551-496b-b488-8e138a4dc459:FAILURE    2020-06-25 09:22:17.299
38c20ac5-dbb7-43ad-b139-f8fde13d0ea5    FAILURE     38c20ac5-dbb7-43ad-b139-f8fde13d0ea5:FAILURE    2020-06-25 09:22:17.300
4a3b3102-d3fa-4c2c-b3cf-46e6822fae5c    FAILURE     4a3b3102-d3fa-4c2c-b3cf-46e6822fae5c:FAILURE    2020-06-25 09:22:17.300
f80bec6d-ab5c-4f63-8aea-cd2f5179195e    FAILURE     f80bec6d-ab5c-4f63-8aea-cd2f5179195e:FAILURE    2020-06-25 09:22:17.300
e75896d2-f314-4423-87be-ea70b2ba5adb    FAILURE     e75896d2-f314-4423-87be-ea70b2ba5adb:FAILURE    2020-06-25 09:22:17.301

If the JSON content is stored in a table, you need an additional APPLY operator:如果 JSON 内容存储在表中,则需要额外的APPLY运算符:

SELECT j.*
-- FROM Dashboardtable d
FROM (VALUES (@idvalue)) d (P_conversations_participants_sessions_flow_outcomes)
OUTER APPLY OPENJSON(d.P_conversations_participants_sessions_flow_outcomes) WITH (
   testId varchar(36) '$.testId',
   testValue varchar(100) '$.testValue',
   test varchar(100) '$.test',
   testStartTimestamp datetime2(3) '$.testStartTimestamp'
) j

As an additional option, if you want to extract scalar values from JSON array, you need to use an index in the path definition:作为附加选项,如果要从 JSON 数组中提取标量值,则需要在path定义中使用索引:

SELECT
   JSON_VALUE(@idvalue, '$[0].testValue') AS testValue,
   JSON_VALUE(@idvalue, '$[0].testStartTimestamp') AS testStartTimestamp,
   JSON_VALUE(@idvalue, '$[0].test') AS test 

Assuming you just want the data out of the variable, then you can use OPENJSON .假设您只想要变量中的数据,那么您可以使用OPENJSON If not, then you need to tell us what you're actually after.如果没有,那么您需要告诉我们您的实际目标。

SELECT *
FROM OPENJSON(@idvalue)
     WITH(testId uniqueidentifier,
          testValue varchar(10),
          test uniqueidentifier,
          testStartTimestamp datetimeoffset(1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM