JSON_QUERY 具有列值
JSON_QUERY with Column values
我的一个表在每一列的每一行中包含 JSON 个值。
数据如下(例.一行)
[{"id":"30a66bec-c0aa-4655-a8ef-506e52bfcc14","type":"nps","value":"promoter","decimalValue":"10"},{"id":"37850b3b-1eac-4921-ae22-b2f6d2450897","type":"sentiment","value":"positive","decimalValue":"0.990000009536743"}]
现在我正在尝试从中检索两列。 (id, 值)
我正在使用 JSON_VALUE
编写以下查询,但在新列的每一行中获取 NULL
值。
select a.jsondata,JSON_VALUE(a.jsondata,'$.id') from table as a
您的 JSON 字段是一个数组,因此您需要指定要查找的元素,假设它始终是您可以使用的第一个元素:
select a.jsondata,JSON_VALUE(a.jsondata,'$[0].id') from table as a
您需要更改方括号内的索引以从 JSON 字符串
中访问您想要的 ID
你有一个 JSON 数组。如果你想把它分成多行,你需要使用 OPENJSON
SELECT j.*
FROM YourTable t
CROSS APPLY OPENJSON (t.Json)
WITH (
id uniqueidentifier,
value varchar(100)
) j;
我的一个表在每一列的每一行中包含 JSON 个值。
数据如下(例.一行)
[{"id":"30a66bec-c0aa-4655-a8ef-506e52bfcc14","type":"nps","value":"promoter","decimalValue":"10"},{"id":"37850b3b-1eac-4921-ae22-b2f6d2450897","type":"sentiment","value":"positive","decimalValue":"0.990000009536743"}]
现在我正在尝试从中检索两列。 (id, 值)
我正在使用 JSON_VALUE
编写以下查询,但在新列的每一行中获取 NULL
值。
select a.jsondata,JSON_VALUE(a.jsondata,'$.id') from table as a
您的 JSON 字段是一个数组,因此您需要指定要查找的元素,假设它始终是您可以使用的第一个元素:
select a.jsondata,JSON_VALUE(a.jsondata,'$[0].id') from table as a
您需要更改方括号内的索引以从 JSON 字符串
中访问您想要的 ID你有一个 JSON 数组。如果你想把它分成多行,你需要使用 OPENJSON
SELECT j.*
FROM YourTable t
CROSS APPLY OPENJSON (t.Json)
WITH (
id uniqueidentifier,
value varchar(100)
) j;