繁体   English   中英

将 json 字符串展平为大查询中的列

[英]Flatten json string into columns in big query

我是 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

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

使用以下方法

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'))    

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

在此处输入图像描述

暂无
暂无

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

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