sql - Bigquery - json_array 来自字段的额外多个元素
问题描述
我的表有一个 JSON 字段,看起来像这样。每个条目中可以有任意数量的评论。
{
"entry": 1234,
"comment_6789": {
"_seconds": 1614864327,
"_nanoseconds": 606000000,
"message": "hello world"
},
"comment_4564564": {
"_seconds": 1614864327,
"_nanoseconds": 606000000,
"message": "hello mars"
}
}
阅读此问题后,我知道我可以使用 JSON_EXTRACT_ARRAY,但我有点不确定如何定位每个comment_
条目,因为它们会有不同的 id?
解决方案
考虑下面
#standardSQL
with `project.dataset.table` as (
select 1 id, '''
{
"entry": 1234,
"comment_6789": {
"_seconds": 1614864327,
"_nanoseconds": 606000000,
"message": "hello world"
},
"comment_4564564": {
"_seconds": 1614864327,
"_nanoseconds": 606000000,
"message": "hello mars"
}
}
''' col
)
select id, comment,
json_value(kv, '$._seconds') seconds,
json_value(kv, '$._nanoseconds') nanoseconds,
json_value(kv, '$.message') message
from `project.dataset.table`,
unnest(regexp_extract_all(col, r'comment_\d+')) comment,
unnest([struct(regexp_extract(col, r'"' || comment || '": ({[^{}]+})') as kv)])
带输出
推荐阅读
- python - 在 Windows 命令提示符上运行 python 脚本
- paypal - 如何传递额外参数以返回 PayPal Connect 的 url
- c# - 如何使用 DataAnnotation 属性在控制台应用程序中验证模型
- ruby-on-rails - Ruby on Rails:Active::RecordNotFound 找不到没有 ID 的文章
- c# - 如何在 C# Rest API 中处理查询参数中的可选 ENUM - 无效和空值以及在 Swagger 中隐藏特定值
- c# - 如何根据 Visual Studio 中的文件名以编程方式更改 C# .cs 文件?
- java - 如何使用google firebase firestore在android中实现实时数据修改?
- sms - 是否可以向来电发送自动短信回复
- c++ - 提升 asio io_service 与 io_context
- python - 使用 vtkAppendPolyData 时如何更改 VTK 中对象的颜色?