查询BigQuery中的JSON值

发布于 2025-01-22 10:32:59 字数 1067 浏览 2 评论 0 原文

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

我希望能够根据 group> group 列中的 group_email JSON值从表中选择行。我检查了docs

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 [0] .group_email')作为group_email 也返回

任何我的想法我做错了吗?

I have a BigQuery table ("activity") where one column ("groups") contains a JSON value (the table is an export of Google Workspace logs).

I'd like to be able to select rows from the table based on the group_email JSON value in the group column. I checked the docs here, but my queries always return null. I tried:

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

but got null. I also tried using the column index of group_email

SELECT
  record_type,
  email,
  JSON_VALUE('groups[4]') AS group_email

but no luck. I tried these same combinations as part of the WHERE statement but got the same results:

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

I also saw this answer but using JSON_EXTRACT_SCALAR('groups', "$.groups[0].group_email") AS group_email also returns null

Any idea what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木緿 2025-01-29 10:32:59

您可以尝试以下方法。

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

您可以参考此 json函数文档以获取更多详细信息。

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

示例数据:

int64_field_0 string_field_1
1 20
2 “这是字符串”
3 {“ id”:10,“ name”:“ Alice”}

查询结果:

You may try below approach.

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

You may refer to this JSON function Documentation for more details.

Please see screenshot of my testing below using the correct syntax for JSON_VALUE.

SAMPLE DATA:

int64_field_0 string_field_1
1 20
2 "This is a string"
3 {"id": 10, "name": "Alice"}

QUERY RESULT:

enter image description here

尐偏执 2025-01-29 10:32:59

虽然我迟到了,但我能够通过

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

though I am late but I am able to get via

SELECT
  record_type,
  email,
  json_extract(groups,'$.group_email') AS group_email
FROM
  `company.workspace_prod.activity`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文