简体   繁体   English

MS SQL 服务器中 JSON 函数路径中的重音字符导致错误

[英]Accent character in path of JSON functions in MS SQL Server cause error

We keep some JSON's in our database as NVARCHAR.我们将一些 JSON 作为 NVARCHAR 保存在我们的数据库中。 Before inserting, we validate JSON with "IsJSON" function.在插入之前,我们使用“IsJSON”function 验证 JSON。 The problem is we have a JSON with Accent character in the keys like "Malārd".问题是我们有一个 JSON 在像“Malārd”这样的键中有重音字符。 IsJson returns 1 which means it is a valid JSON but when we try to use it in a path such as '$.Malārd' it will cause below error: IsJson 返回 1 这意味着它是一个有效的 JSON 但是当我们尝试在诸如 '$.Malārd' 之类的路径中使用它时,它将导致以下错误:

-- QUERY
SELECT JSON_VALUE(N'{"Malārd":1000}', N'$.Malārd')
-- OUTPUT
-- JSON path is not properly formatted. Unexpected character 'ā' is found at position 5

If I have to, I will prevent inserting accent characters in keys, but I hope to find a better solution for that.如果必须,我会阻止在键中插入重音字符,但我希望为此找到更好的解决方案。

I know that I can replace accent character and use trick like COLLATE LATIN1_GENERAL_CS_AI but we have some infrastructure tools and API's in our project which allowed client developer insert and grabs a variety of JSON models.我知道我可以替换重音字符并使用 COLLATE LATIN1_GENERAL_CS_AI 之类的技巧,但我们的项目中有一些基础设施工具和 API,允许客户端开发人员插入并获取各种 JSON 模型。

You need to surround path expression with quotes if the key name starts with a dollar sign or contains special characters:如果键名以美元符号开头或包含特殊字符,则需要用引号将路径表达式括起来:

SELECT JSON_VALUE(N'{"Malārd":1000}', N'$."Malārd"')

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

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