简体   繁体   English

我可以将包含字符串化数组的行值转换为 BigQuery 中的数组吗

[英]Can I convert row value contains of stringified array to array in BigQuery

I have a row which the value is the string of array.我有一行,其值是数组的字符串。 I need to convert it to the array.我需要将其转换为数组。

在此处输入图像描述

I tried to convert it by strip the quote using SUBSTR function.我尝试通过使用SUBSTR function 去除引号来转换它。

WITH k AS (SELECT 1 as id, (SELECT after.ClientIds   
FROM `kube-playground.events.login_v2_users`) AS c)

Select id, ARRAY(SELECT * FROM UNNEST(SPLIT(SUBSTR(c, 2 , LENGTH(c) - 2)))) AS x from k

It is failed because Scalar subquery produced more than one element.它失败了,因为标量子查询产生了不止一个元素。 Any tips or ideas for this problem?这个问题有什么提示或想法吗? Thank you谢谢

Below is for BigQuery Standard SQL以下是 BigQuery 标准 SQL

You can simply use JSON_EXTRACT_ARRAY as in below example您可以简单地使用JSON_EXTRACT_ARRAY ,如下例所示

#standardSQL
SELECT JSON_EXTRACT_ARRAY(ClientIds)
FROM `kube-playground.events.login_v2_users`

You can test it as in below您可以按照下面的方式进行测试

#standardSQL
WITH `kube-playground.events.login_v2_users` AS (
  SELECT '[{"time":"asd", "id":"qwe"},{"time":"asd2", "id":"qwe2"},{"time":"asd3", "id":"qwe3"}]' ClientIds UNION ALL
  SELECT '[{"time":"aseft", "id":"sdf"},{"time":"aseft2", "id":"sdf2"}]'
)
SELECT JSON_EXTRACT_ARRAY(ClientIds)
FROM `kube-playground.events.login_v2_users`

with result结果

Row f0_  
1   {"time":"asd","id":"qwe"}    
    {"time":"asd2","id":"qwe2"}  
    {"time":"asd3","id":"qwe3"}  
2   {"time":"aseft","id":"sdf"}  
    {"time":"aseft2","id":"sdf2"}    

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

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