这让我发疯,我不明白我的方法有什么问题。
我在 SQL 中生成一个 JSON 对象,如下所示:
select @output = (
select distinct lngEmpNo, txtFullName
from tblSecret
for json path, root('result'), include_null_values
)
我得到这样的结果:
{"result":[{"lngEmpNo":696969,"txtFullName":"Clinton, Bill"}]}
ISJSON()
确认它是有效的 JSON,并且 JSON_QUERY(@OUTPUT, '$.result')
将返回数组 []
JSON 对象的一部分……太酷了!但是,我正在尝试使用
JSON_QUERY
提取特定值:这让我得到一个
NULL
值(value)。 为什么??????? 我已经用 [0]
试过了,没有 [0]
,当然还有 txtFullName[0]
SELECT JSON_QUERY(@jsonResponse, '$.result[0].txtFullName');
我以
strict
为前缀, SELECT JSON_QUERY(@jsonResponse, 'strict $.result[0].txtFullName');
,它告诉我:Msg 13607, Level 16, State 4, Line 29
JSON path is not properly formatted. Unexpected character 't' is found at
position 18.
我究竟做错了什么?我的结构有什么问题?
最佳答案
JSON_QUERY
只会提取 对象 或 阵列 .您正在尝试提取单个 值 所以,你需要使用 JSON_VALUE
.例如:
SELECT JSON_VALUE(@jsonResponse, '$.result[0].txtFullName');
关于json - 为什么 JSON_QUERY 发回空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50862373/