如何从 BigQuery 中没有键的 JSON 读取数据?
Posted
技术标签:
【中文标题】如何从 BigQuery 中没有键的 JSON 读取数据?【英文标题】:How to read the data from a JSON which is not having keys in BigQuery? 【发布时间】:2020-10-22 10:24:04 【问题描述】:我有这种格式的数据
"OgDKrh1hT6NzBHXsjh7n8yrkrE62":["U2","X1","D1","C3","H2","D3","O3","Q1","A2"],"UxdTolmzq9cAYjPkTX57VAzas2w1":["E3","S1","V2","W1","M1","H3","L3","G2","Y3"]
我想要这种格式的数据
id details
OgDKrh1hT6NzBHXsjh7n8yrkrE62 U2,X1,D1,C3,H2,D3,O3,Q1,A2
UxdTolmzq9cAYjPkTX57VAzas2w1 E3,S1,V2,W1,M1,H3,L3,G2,Y3
【问题讨论】:
请说明表格的格式。它们是什么列和什么数据类型! 假设该表只有一列“raw_data”,数据以字符串格式存储。 当然。看答案 【参考方案1】:以下是 BigQuery 标准 SQL
#standardSQL
select
arr[offset(0)] as id,
arr[offset(1)] as details
from `project.dataset.table`,
unnest(regexp_extract_all(text, r'"[^"]+":\[[^\[]+\]')) kv,
unnest([struct(split(translate(kv, '"[]', ''), ':') as arr)])
如果将上面应用于您的问题中的样本数据,如下例所示
#standardSQL
with `project.dataset.table` as (
select '"OgDKrh1hT6NzBHXsjh7n8yrkrE62":["U2","X1","D1","C3","H2","D3","O3","Q1","A2"],"UxdTolmzq9cAYjPkTX57VAzas2w1":["E3","S1","V2","W1","M1","H3","L3","G2","Y3"]' text
)
select
arr[offset(0)] as id,
arr[offset(1)] as details
from `project.dataset.table`,
unnest(regexp_extract_all(text, r'"[^"]+":\[[^\[]+\]')) kv,
unnest([struct(split(translate(kv, '"[]', ''), ':') as arr)])
输出是
【讨论】:
以上是关于如何从 BigQuery 中没有键的 JSON 读取数据?的主要内容,如果未能解决你的问题,请参考以下文章
在 json 文档中没有指定键的 bigquery 中从 json 字符串中提取键和值
解析列中具有动态键的 JSON 值并将 JSON 转换为 BigQuery 中的记录列结构
BigQuery 加载 JSON 文件:如何忽略或重命名字段?