[英]Escaping Characters in Bigquery json_extract() function
when using Google's BigQuery , there's a function that can extract elements from json strings using jsonPath.使用Google 的 BigQuery时,有一个function可以使用 jsonPath 从 json 字符串中提取元素。 For example:
例如:
SELECT JSON_EXTRACT(data,"$.key.value") AS feature FROM tablename
when the json key itself contains a dot, {"key.value":"value"}
It's not clear how to escape that properly.当 json 键本身包含一个点
{"key.value":"value"}
时,尚不清楚如何正确转义它。
this jsonpath message board question says that jsonpath itself supports this format这个 jsonpath留言板问题说 jsonpath 本身支持这种格式
@Test
public void path_with_bracket_notation() throws Exception {
String json = "{\"foo.bar\": {\"key\": \"value\"}}";
Assert.assertEquals("value", JsonPath.read(json, "$.['foo.bar'].key"));
However in bigquery this type of espcaping attempts cause Error: JSONPath parse error
errors.但是在 bigquery 中,这种类型的转义尝试会导致
Error: JSONPath parse error
错误。
Update, new answer: 更新,新答案:
BigQuery's JSON_EXTRACT
and JSON_EXTRACT_SCALAR
functions now support JSON bracket notation in JSONPath, so the following query works: BigQuery的
JSON_EXTRACT
和JSON_EXTRACT_SCALAR
函数现在支持JSON_EXTRACT_SCALAR
JSON括号表示法,因此以下查询有效:
SELECT JSON_EXTRACT('{"key.value": {"foo": "bar"}}', "$['key.value']")
and returns 并返回
{"foo":"bar"}
Old, now outdated answer: 旧的,现在过时的答案:
Unfortunatelly BigQuery does not support escaping special characters in json path. 不幸的是,BigQuery不支持在json路径中转义特殊字符。 The workaround would be to use REPLACE function to convert dots to underscores, ie
解决方法是使用REPLACE函数将点转换为下划线,即
SELECT
json_extract(
replace('{"key.value":"value"}',
'key.value',
'key_value'),
'$.key_value')
use backtick to escape (it is also used to escape hyphen in project/dataset name)使用反引号转义(它也用于转义项目/数据集名称中的连字符)
SELECT JSON_VALUE(json_field.`key.value`) AS feature FROM tablename
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.