如何从 BigQuery 中的 JSON 字符串中提取数组

Posted

技术标签:

【中文标题】如何从 BigQuery 中的 JSON 字符串中提取数组【英文标题】:How to extract array from JSON String in BigQuery 【发布时间】:2021-10-13 14:47:40 【问题描述】:
Hi, I'm working on a table looks like below:
----------------------------------------------------------------------------------------------------------------------------------------------
|     user_id    |                       j_games_information                                                   
----------------------------------------------------------------------------------------------------------------------------------------------
|   hsbdgcy76s   |"data": ["game_id": "acb", "rewards":["no":3,"items":"oils"],"game_id": "bsm", "rewards":["no":4,"items":"bombs"]]    
----------------------------------------------------------------------------------------------------------------------------------------------
|   kslcn6vg76   |"data": ["game_id": "ohf", "rewards":["no":6,"items":"oils"],"game_id": "dfg", "rewards":["no":7,"items":"bombs"]]   
----------------------------------------------------------------------------------------------------------------------------------------------

我的预期输出将是:

-----------------------------------
|     user_id    |     game_ids   |                                             
-----------------------------------
|   hsbdgcy76s   |      acb       |   
-----------------------------------
|   hsbdgcy76s   |      bsm       |   
-----------------------------------
|   kslcn6vg76   |      ohf       |   
-----------------------------------
|   kslcn6vg76   |      dfg       |   
-----------------------------------

我尝试了以下代码,但此查询未返回任何结果。谁能帮我这个?谢谢!

select user_id, JSON_EXRACT_SCALAR(json_array,"$.game_id") AS game_ids
from table, unnest(json_extract_array(j_games_information,"$.data")) AS json_array

但是

【问题讨论】:

【参考方案1】:

尝试用j_games_information替换json_file

with mytable as (
    select 'hsbdgcy76s' as user_id, '"data": ["game_id": "acb", "rewards":["no":3,"items":"oils"],"game_id": "bsm", "rewards":["no":4,"items":"bombs"]]' as j_games_information union all 
    select 'kslcn6vg76' as user_id, '"data": ["game_id": "ohf", "rewards":["no":6,"items":"oils"],"game_id": "dfg", "rewards":["no":7,"items":"bombs"]]' as j_games_information
)
select user_id, JSON_EXTRACT_SCALAR(json_array,"$.game_id") AS game_ids
from mytable, unnest(json_extract_array(j_games_information,"$.data")) AS json_array

【讨论】:

Ops,我实际上使用了j_games_information。 嗯,你的查询对我有用

以上是关于如何从 BigQuery 中的 JSON 字符串中提取数组的主要内容,如果未能解决你的问题,请参考以下文章

如何计算位于 BigQuery 表中的 json 数组中的对象数?

如何从 Bigquery 中的这个嵌套 JSON 类型列中查询特定的内容

从 Bigquery 中的 json 获取数组

使用 Java 从 BigQuery 到 BigQuery 表的表复制中的 JSON 解析错误

从 BigQuery 中的 JSON 数组中提取多个值

如何从bigquery中的字符串中提取子集