在 BigQuery 中查询 JSON 值

问题描述 投票:0回答:2

我有一个 BigQuery 表(“活动”),其中一列(“组”)包含一个 JSON 值(该表是 Google Workspace 日志的导出)。

我希望能够根据

group_email
列中的
group
JSON 值从表中选择行。我检查了文档here,但我的查询总是返回 null。我试过:

SELECT
  record_type,
  email,
  JSON_VALUE('groups.group_email') AS group_email

但结果为空。我还尝试使用

group_email

的列索引
SELECT
  record_type,
  email,
  JSON_VALUE('groups[4]') AS group_email

但运气不好。我尝试了这些相同的组合作为 WHERE 语句的一部分,但得到了相同的结果:

SELECT
  *
FROM
  `company.workspace_prod.activity`
WHERE
  record_type = 'groups'
  and
  JSON_VALUE('groups.group_email') = '[email protected]'
LIMIT
  10

我也看到了这个答案但是使用

JSON_EXTRACT_SCALAR('groups', "$.groups[0].group_email") AS group_email
也返回null

知道我做错了什么吗?

sql google-bigquery google-workspace
2个回答
3
投票

您可以尝试以下方法。

SELECT
  record_type,
  email,
  JSON_VALUE(groups,'$.group_email') AS group_email
FROM
  `company.workspace_prod.activity`

您可以参考这个JSON函数文档了解更多详情。

请参阅下面我使用 JSON_VALUE 的正确语法进行测试的屏幕截图。

样本数据:

int64_field_0 字符串_字段_1
1 20
2 “这是一个字符串”
3 {“id”:10,“名字”:“爱丽丝”}

查询结果:


0
投票

虽然我迟到了但我还是能过去

SELECT
  record_type,
  email,
  json_extract(groups,'$.group_email') AS group_email
FROM
  `company.workspace_prod.activity`
© www.soinside.com 2019 - 2024. All rights reserved.