很多人都有这个问题,但是这通常是因为JSON是不正确的。有人能告诉我为什么这不管用吗。我希望得到以下结果集:
ONE FirstName LastName
1 John Smith
Instead, I get an empty result set and the following error message in SSMS:
Msg 13609, Level 16, State 2, Line 20
JSON text is not properly formatted. Unexpected character 'J' is found at position 0.
DECLARE @IN_JSON NVARCHAR(MAX) =
N'{
"resume": {
"mailAddress": {
"sectionOrder": 10,
"section": {
"firstName": "John",
"lastName": "Smith"
}
}
}
}'
SELECT '1' AS [One],
JSON_VALUE(a.value, '$.firstName') AS [FirstName],
JSON_VALUE(a.value, '$.lastName') AS [LastName]
FROM OPENJSON (@IN_JSON, '$.resume.mailAddress.section') AS a
发布于 2022-07-26 20:30:39
-- A colleague showed me the following which works as well. And thank you again Barbaros for the very fast response.
DECLARE @IN_JSON NVARCHAR(MAX) =
N'{
"resume": {
"mailAddress": {
"sectionOrder": 10,
"section": {
"firstName": "John",
"lastName": "Smith"
}
}
}
}'
SELECT '1' AS [One]
, JSON_VALUE(@IN_JSON, '$.resume.mailAddress.section.firstName') AS [FirstName]
, JSON_VALUE(@IN_JSON, '$.resume.mailAddress.section.lastName') AS [LastName]
-- Output result set:
-- One FirstName LastName
-- 1 John Smith
https://stackoverflow.com/questions/73117629
复制相似问题