【问题标题】:Flatten json string into columns in big query将 json 字符串展平为大查询中的列
【发布时间】:2022-07-07 23:00:28
【问题描述】:

我是 bigquery 的新手,正在尝试将下面的 JSON 字符串扁平化为单独的列。

{“城市”:“”,“国家”:“美国”,“电子邮件”:“test@gmail.com”,“省”:“”,“州”:“”}

column_json,
json_extract_scalar(h, '$.city') as city
from 
table as l
left join unnest(json_extract_array(column_json)) as h

我尝试了上面的代码,但我得到的只是空值。我该怎么办?任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: sql json google-bigquery


    【解决方案1】:

    使用下面的方法

    create temp function  get_keys(input string) returns array<string> language js as """
      return Object.keys(JSON.parse(input));
      """;
    create temp function  get_values(input string) returns array<string> language js as """
      return Object.values(JSON.parse(input));
      """;
    select * except (json) from (
      select json, key, value
      from your_table, 
      unnest(get_keys(json)) key with offset
      join unnest(get_values(json)) value with offset
      using(offset)
    )
    pivot (any_value(value) for key in ('city', 'country', 'email', 'province', 'state'))    
    

    如果适用于您问题中的样本数据 - 输出是

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 2020-09-10
      • 1970-01-01
      • 2022-10-23
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      相关资源
      最近更新 更多