[英]How to fix SQL server JSON_VALUE path has '-' error
I'm trying to get values of fields in sql that is in JSON type.我正在尝试获取 sql 中 JSON 类型的字段值。 unfortunately path has '-' so that I get an error when I try to get the value.
不幸的是,路径有“-”,因此在尝试获取值时出现错误。
field->** **{"Con":["AA"],"X-Location-Latitude":["55.06"]}
When I try to get json value with below query当我尝试使用以下查询获取 json 值时
SELECT JSON_VALUE(field, '$.X-Location-Latitude')
FROM table
I get this error我收到这个错误
Json path is not properly formatted unexpected character '-' is found
Json 路径格式不正确 发现意外字符“-”
I also tried adding double quotes as我也尝试添加双引号作为
SELECT JSON_VALUE(field, '$."X-Location-Latitude"')
FROM table
and this returned NULL.这返回了 NULL。
Does anyone have a solution?有没有人有办法解决吗?
So you originally posted:所以你最初发布:
field-> {"Con":"[AA]","X-Location-Latitude":"[55]"}
"[55]"
is a single string of length 4; "[55]"
是一个长度为4的字符串; '$."X-Location-Latitude"'
is correct for that single value. '$."X-Location-Latitude"'
对于该单个值是正确的。
I see now that you've changed it to:我现在看到您已将其更改为:
{"Con":["AA"],"X-Location-Latitude":["55.06"]}
["55.06"]
is a completely different thing (and not because of the .06
); ["55.06"]
是完全不同的东西(不是因为.06
); it's an array of strings , with one element "55.06"
.它是一个字符串数组,包含一个元素
"55.06"
。 This makes all the difference to JSON_VALUE这对 JSON_VALUE 产生了重大影响
JSON_VALUE cannot be used with a path that selects an array; JSON_VALUE 不能与选择数组的路径一起使用; you'll have to nominate a single value in the array, for example:
您必须在数组中指定一个值,例如:
SELECT JSON_VALUE(field, '$."X-Location-Latitude"[0]') FROM table
^^^
first array element
JSON_VALUE must be used with scalar (single value) properties, not collections/arrays(things enclosed in []
) or complex objects (things enclosed in {}
) JSON_VALUE 必须与标量(单值)属性一起使用,而不是集合/数组(包含在
[]
中的东西)或复杂对象(包含在{}
中的东西)
The fine manual has a nice table about what happens when you try select various things with JSON_VALUE, in lax and strict mode 精美的手册有一个很好的表格,说明当您在松散和严格模式下使用 JSON_VALUE 尝试 select 各种事情时会发生什么
Replacing JSON_VALUE with JSON_QUERY fixed my problem.用JSON_QUERY替换JSON_VALUE解决了我的问题。
So below code works fine所以下面的代码工作正常
SELECT JSON_QUERY(field, '$."X-Location-Latitude"') FROM table
Returns ["55.06"]返回 ["55.06"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.