我有一个如下的bigquery表
idx | 信息 |
---|---|
1 | {'列':['姓名','年龄'], 数据:[['谢尔顿', 29], ['拉吉',28]]} |
2 | {'列':['姓名','年龄'], 数据:[['伦纳德', 28], ['霍华德',29]]} |
info 是一个字符串列
我想像下面这样将其解除嵌套
idx | 名字 | 年龄 |
---|---|---|
1 | 谢尔顿 | 29 |
1 | 拉杰 | 28 |
2 | 伦纳德 | 28 |
2 | 霍华德 | 29 |
此查询正是您正在寻找的:
WITH data AS (
SELECT 1 AS idx, '{"columns":["name","age"],"data":[["Sheldon",29],["Raj",28]]}' AS info
UNION ALL
SELECT 2 AS idx, '{"columns":["name","age"],"data":[["Leonard",28],["Howard",29]]}' AS info
)
SELECT
idx,
JSON_EXTRACT_SCALAR(entry, '$[0]') AS name,
CAST(JSON_EXTRACT_SCALAR(entry, '$[1]') AS INT64) AS age
FROM data,
UNNEST(JSON_EXTRACT_ARRAY(info, '$.data')) AS entry
我建议您阅读有关 JSON 函数的 BigQuery 官方文档