简体   繁体   English

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

[英]Extract multiple values from an array in JSON in BigQuery

I have a JSON in my database table like the following one, and let's say the column that contains a JSON file called favorites.我的数据库表中有一个 JSON,如下所示,假设该列包含一个名为 favorites 的 JSON 文件。

{
    "info": {
        "music": [{"Year":2021,"Name":"Stay","Singer":"Justin Bieber"},
                  {"Year":2015,"Name":"Love Yourself","Singer":"Justin Bieber"},
                  {"Year":2003,"Name":"Crazy In Love","Singer":"Beyonce"}
                 ],
        "movie": [{"Year":2018,"Name":"Green Book","Director":"Peter Farrelly"},
                  {"Year":2007,"Name":"Lust, Caution","Director":"Ang Lee"}
                 ]
             }
}

I wanted to select all values from tags and my expected table would be like as following:

-----------------------------------------------------------------------------
|             Name                    |                      Singer         |
----------------------------------------------------------------------------
|   Stay,Love Yourself,Crazy In Love  |  Justin Bieber,Justin Bieber,Beyonce|
-----------------------------------------------------------------------------

I already know how to get the first value in an array with JSON_QUERY(json_col,'$.info.music[0].Name'), but I would like to extracted all the names or singers into one single column, and some arrays may have multiple items.我已经知道如何使用 JSON_QUERY(json_col,'$.info.music[0].Name') 获取数组中的第一个值,但我想将所有名称或歌手提取到一个列中,还有一些 arrays可能有多个项目。 Doe anyone have any suggestions?有人有什么建议吗?

Consider below approach考虑以下方法

select 
  array(select json_extract_scalar(x, '$.Name') from unnest(json_extract_array(json_col, '$.info.music') || json_extract_array(json_col, '$.info.movie')) x) Name,
  array(select json_extract_scalar(x, '$.Singer') from unnest(json_extract_array(json_col, '$.info.music')) x) Singer
from data      

if applied to sample data in your question - output is如果应用于您问题中的样本数据 - output 是

在此处输入图像描述

I just realized - you wanted comma separated list - so consider below then我刚刚意识到 - 你想要逗号分隔列表 - 那么请考虑下面

select 
  (select string_agg(json_extract_scalar(x, '$.Name')) from unnest(json_extract_array(json_col, '$.info.music') || json_extract_array(json_col, '$.info.movie')) x) Name,
  (select string_agg(json_extract_scalar(x, '$.Singer')) from unnest(json_extract_array(json_col, '$.info.music')) x) Singer
from data     

with output与 output

在此处输入图像描述

Another sol.另一个溶胶。 One can use ARRAY_TO_STRING if you don't want aggregation.如果您不想聚合,可以使用 ARRAY_TO_STRING。

with data as
(
select
    """
    {
"info": {
        "music": [{"Year":2021,"Name":"Stay","Singer":"Justin Bieber"},
                  {"Year":2015,"Name":"Love Yourself","Singer":"Justin Bieber"},
                  {"Year":2003,"Name":"Crazy In Love","Singer":"Beyonce"}
                 ],
        "movie": [{"Year":2018,"Name":"Green Book","Director":"Peter Farrelly"},
                  {"Year":2007,"Name":"Lust, Caution","Director":"Ang Lee"}
                 ]
             }
}
""" as _json
)

select array_to_string(
    array(
    select  json_extract_scalar(x,"$.Name")
    from  data,  
          unnest(json_extract_array(_json,"$.info.music")) as x
),"," 
) as Name, array_to_string(
    array(
      select  json_extract_scalar(x,"$.Singer")
    from  data,  
        unnest(json_extract_array(_json,"$.info.music")) as x
),","
) as Singer

Reesult结果在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM