从 JSON 中提取值,键名在 bigquery 中包含 #

ton*_*kix 3 json google-bigquery

我在 bigquery 中有一个名为 test 的表。它有一个名为 的列attributes。该值采用 JSON 格式,其中“# of items”为键,例如:{"# of items": "100"}

当我执行下面的查询时 SELECT JSON_EXTRACT(attributes, "$['# of items']") AS num_items FROM test

我遇到了以下错误: Error: JSONPath parse error at: ['# of items']

获取其价值的最简单方法是什么?

Mik*_*ant 5

BigQuery 旧版 SQL 失败

改用 BigQuery Standard SQL 就可以了

#standardSQL
WITH `project.dataset.test` AS (
  SELECT '{"# of items": "100"}' AS attributes
)
SELECT JSON_EXTRACT(attributes, "$['# of items']") AS num_items
FROM `project.dataset.test`   
Run Code Online (Sandbox Code Playgroud)

结果为

Row num_items    
1   "100"    
Run Code Online (Sandbox Code Playgroud)

如果由于某种原因您必须使用 BQ Legacy SQL - 使用以下技巧

#legacySQL
SELECT JSON_EXTRACT(REPLACE(attributes, '"# of items"', "'number of items'"), "$['number of items']") AS num_items
FROM [project:dataset.test]
Run Code Online (Sandbox Code Playgroud)